<?php
// $Id: nodewords.test,v 1.1.2.2 2009/11/26 21:36:24 ilo Exp $

/**
 * @file
 * Test file for nodewords module.
 */

/**
 * Nodewords Test Case implementation.
 */
class NodewordsFunctionalityTestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name'        => 'Nodewords functionality test',
      'description' => 'Test basic nodewords functionality.',
      'group'       => 'nodewords'
    );
  }

  function setUp() {
    // Enable minimum required modules.
    parent::setUp('nodewords', 'nodewords_basic');
  }

  /**
   * Verify edit meta tag permissions. Configure the nodewords module to expose
   * several meta tags, and allow a user to edit only a few of them. Verify that
   * only allowed meta tags are available for the user and that value is saved
   * and showed on node view.
   *
   * Verify that user without "administer nodes" permission is able to edit node
   * meta tags (issue: http://drupal.org/node/588946).
   */
  function testMetaTagsPermissionFunctionality() {

    // Create an administrator user. This use will configure the nodewords
    // settings according to the test.
    $permissions = array(
      'administer meta tags',
    );
    $user = $this->drupalCreateUser($permissions);

    // Log in to Drupal using the administrator user.
    $this->drupalLogin($user);

    // Enable some meta tags
    $options = array(
      // Allow editing KEYWORDS, DESCRIPTION and COPYRIGHT
      'nodewords_edit[keywords]' => 1,
      'nodewords_edit[description]' => 1,
      'nodewords_edit[copyright]' => 1,
      // Only show DESCRIPTION and COPYRIGHT meta tags
      'nodewords_head[copyright]' => 1,
      'nodewords_head[description]' => 1,
    );
    $this->drupalPost('admin/content/nodewords', $options, t('Save configuration'));
    $this->assertRaw(t('The configuration options have been saved.'), t('Meta tags configuration saved.'));

    // Create an user with permission to create a page node, and to edit the
    // meta tags COPYRIGHT, and DESCRIPTION.
    $permissions = array(
      'create page content',
      'edit own page content',
      'delete own page content',
      'edit meta tag COPYRIGHT',
      'edit meta tag DESCRIPTION',
    );   
    $user = $this->drupalCreateUser($permissions);

    // Log in to Drupal using the previously created user.
    $this->drupalLogin($user);

    // Verify that only COPYRIGHT and DESCRIPTION meta tags are available for
    // this user due to edit meta tag permissions, even if other meta tags are
    // available for editing.
    $this->drupalGet('node/add/page');
    $this->assertField('nodewords[copyright][value]', t('Copyright meta tag field found.'));
    $this->assertField('nodewords[description][value]', t('Description meta tag field found.'));
    $this->assertNoField('nodewords[keywords][value]', t('Keywords meta tag field not found.'));

    // Create a page node with random meta tag values
    $options = array(
      'title' => $this->randomName(),
      'nodewords[copyright][value]' => $this->randomName(),
      'nodewords[description][value]' => $this->randomName(),
    );
    $this->drupalPost('node/add/page', $options, t('Save'));
    $this->assertRaw(t('Page %title has been created.', array('%title' => $options['title'])), t('Page successfully saved.'));

    // Check for meta information in the node/% page.
    $this->assertRaw('<meta name="copyright" content="' . $options['nodewords[copyright][value]'] . '" />', t('Copyright meta tag successfully verified.'));
    $this->assertRaw('<meta name="description" content="' . $options['nodewords[description][value]'] . '" />', t('Description meta tag successfully verified.'));

    // Update page: change copyright meta tag
    $options = array(
      'title' => $this->randomName(),
      'nodewords[copyright][value]' => '',
      'nodewords[description][value]' => $this->randomName(),
    );
    $this->drupalPost($this->getUrl() . '/edit', $options, t('Save'));
    $this->assertRaw(t('Page %title has been updated.', array('%title' => $options['title'])), t('Page successfully saved.'));

    // Check for meta information again (update operation).
    $this->assertRaw('<meta name="description" content="' . $options['nodewords[description][value]'] . '" />', t('Description meta tag successfully verified.'));
    // Copyright Meta tag is not set, and should not exist.
    $this->assertNoRaw('<meta name="copyright" ', t('Copyright meta tag successfully verified.'));
  }

}


/**
 * Nodewords Search integration Test Case implementation.
 */
class NodewordsSearchTestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name'        => 'Nodewords Search integration',
      'description' => 'Test nodewords searching feature functionality.',
      'group'       => 'nodewords'
    );
  }

  function setUp() {
    // Enable minimum required modules.
    parent::setUp('nodewords', 'nodewords_basic', 'search');
  }

  /**
   * Verify that search works for KEYWORDS and DESCRIPTION meta tags
   *
   * Create two nodes with KEYWORDS and DESCRIPTION meta tags and does search
   * for both tags and both modules.
   *
   * Also tests the issue: http://drupal.org/node/628868
   */
  function testSearchMetaTagsFunctionality() {

    // Create an administrator user.
    $permissions = array(
      'administer content types',
      'administer meta tags',
      'administer search',
      'administer site configuration'
    );
    $admin_user = $this->drupalCreateUser($permissions);

    // Log in to Drupal using the administrator user.
    $this->drupalLogin($admin_user);

    // Enable the tags KEYWORDS, DESCRIPTION and COPYRIGHT for editing and
    // to rendered in the output.
    $options = array(
      'nodewords_edit[keywords]' => 1,
      'nodewords_edit[description]' => 1,
      'nodewords_edit[copyright]' => 1,
      'nodewords_head[keywords]' => 1,
      'nodewords_head[description]' => 1,
      'nodewords_head[copyright]' => 1,
    );
    $this->drupalPost('admin/content/nodewords', $options, t('Save configuration'));

    // Check that page content type has been updated.
    $this->assertRaw(t('The configuration options have been saved.'));

    // Create an user with permission to create a page node, and to edit the
    // meta tags COPYRIGHT, and DESCRIPTION.
    $permissions = array(
      'create page content',
      'edit meta tag KEYWORDS',
      'edit meta tag DESCRIPTION',
      'search content',
    );
    $user = $this->drupalCreateUser($permissions);

    // Log in to Drupal using the previously created user.
    $this->drupalLogin($user);

    // Create two page nodes with meta tags.
    $options1 = array(
      'title' => $this->randomName(),
      'nodewords[keywords][value]' => $this->randomName(),
      'nodewords[description][value]' => $this->randomName(),
    );
    $this->drupalPost('node/add/page', $options1, t('Save'));
    // Check for meta information in the node/% page.
    $this->assertRaw('<meta name="keywords" content="' . $options1['nodewords[keywords][value]'] . '" />', t('Keywords meta tag successfully verified.'));
    $this->assertRaw('<meta name="description" content="' . $options1['nodewords[description][value]'] . '" />', t('Description meta tag successfully verified.'));

    // Create two page nodes with meta tags.
    $options2 = array(
      'title' => $this->randomName(),
      'nodewords[keywords][value]' => $this->randomName(),
      'nodewords[description][value]' => $this->randomName(),
    );
    $this->drupalPost('node/add/page', $options2, t('Save'));
    // Check for meta information in the node/% page.
    $this->assertRaw('<meta name="keywords" content="' . $options2['nodewords[keywords][value]'] . '" />', t('Keywords meta tag successfully verified.'));
    $this->assertRaw('<meta name="description" content="' . $options2['nodewords[description][value]'] . '" />', t('Description meta tag successfully verified.'));

    // Login again as admin user..
    $this->drupalLogin($admin_user);

    // Launch cron, should re-index the site content.
    $this->drupalGet('admin/reports/status/run-cron');
    $this->assertRaw(t('Cron ran successfully.'));

    // Verify all content has been indexed.
    $this->drupalGet('admin/settings/search');
    $this->assertText(t('100% of the site has been indexed'));

    // Log in again as a regular user, has permissions to search content.
    $this->drupalLogin($user);

    // Search for nodewords of node 1: use description meta tag
    $this->drupalGet('search/node/' . $options1['nodewords[description][value]']);
    $this->assertText($options1['title'], t('Search node using description verified successfully.'));

    // Search for nodewords of node 1: use keywords meta tag
    $this->drupalGet('search/node/' . $options1['nodewords[keywords][value]']);
    $this->assertText($options1['title'], t('Search node using keywords verified successfully.'));

    // Search for nodewords of node 2: use description meta tag
    $this->drupalGet('search/node/' . $options2['nodewords[description][value]']);
    $this->assertText($options2['title'], t('Search node using description verified successfully.'));

    // Search for nodewords of node 2: use keywords meta tag
    $this->drupalGet('search/node/' . $options2['nodewords[keywords][value]']);
    $this->assertText($options2['title'], t('Search node using keywords verified successfully.'));
  }

}