<?php
/**
 * @file
 * Functional tests for the Metatag:Context module.
 */

class MetatagContextTestCase extends DrupalWebTestCase {
  /**
   * The getInfo() method provides information about the test.
   * In order for the test to be run, the getInfo() method needs
   * to be implemented.
   */
  public static function getInfo() {
    return array(
      'name' => 'Meta tag context tests',
      'description' => 'Test basic meta tag context functionality.',
      'group' => 'Metatag',
    );
  }

  /**
   * Prepares the testing environment
   */
  public function setUp(array $modules = array()) {
    $modules[] = 'metatag';
    $modules[] = 'metatag_context';
    parent::setUp($modules);
    // Create user.
    $this->privileged_user = $this->drupalCreateUser(array(
      'bypass node access',
      'administer content types',
      'administer meta tags',
    ));
    $this->drupalLogin($this->privileged_user);
    // Create content type, with underscores.
    $type_name = strtolower($this->randomName(8)) . '_test';
    $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
    $this->type = $type->type;
    // Store a valid URL name, with hyphens instead of underscores.
    $this->hyphen_type = str_replace('_', '-', $this->type);
  }

  /**
   * Performs the basic tests.
   */
  public function testMetatagContextBasic() {
    // Create content type node.
    $this->drupalPost('node/add/' . $this->hyphen_type, array('title' => $this->randomName(8)), t('Save'));
    $this->context_name = drupal_strtolower($this->randomName(8));

    // Generate metatags and check content.
    $this->metatag_pages['node'] = $this->createMetatagObject('node/1', 'node_metatags');
    $this->metatag_pages['page'] = $this->createMetatagObject('<front>', 'frontpage_metatags');
    foreach ($this->metatag_pages as $page) {
      $this->generateMetatag($page);
      $this->checkMetatags($page);
    }

    // Edit metatag and check content.
    $this->metatag_pages['node']->title = 'New title';
    $this->metatag_pages['node']->description = '';
    $this->editMetatag($this->metatag_pages['node']);
    $this->checkMetatags($this->metatag_pages['node']);

  }

  /**
   * Creates a metatag object which can be used for generate and check
   * the metatag_context module behavior.
   *
   * @param $path
   *   Path where generate metatags.
   * @param $identifier
   *   Custom test to identify metatags in source code.
   *
   * @return $metatag_object
   *   Metatag mapping object.
   */
  function createMetatagObject($path, $identifier) {
    $metatag_object = new stdClass();
    $metatag_object->name = drupal_strtolower($this->randomName(10));
    $metatag_object->path = $path;
    $metatag_object->title = "My $identifier title";
    $metatag_object->description = "My $identifier description";
    $metatag_object->abstract = "My $identifier abstract";
    $metatag_object->keywords = "My $identifier keywords";

    return $metatag_object;
  }

  /**
   * Generates metatags by path from a metatag_object instance.
   *
   * @return $metatag_object
   *   Metatag mapping object.
   */
  function generateMetatag($metatag_object) {
    //Add new Metatag object by path.
    $edit = array(
      'name' => $metatag_object->name,
    );
    $this->drupalPost('admin/config/search/metatags/context/add', $edit, t('Add and configure'));
    $this->editMetatag($metatag_object);
  }

  /**
   * Edits metatags by path from a metatag_object instance.
   *
   * @return $metatag_object
   *   Metatag mapping object.
   */
  function editMetatag($metatag_object) {
    $edit_metatag = array(
      'paths' => $metatag_object->path,
      'metatags[und][title][value]' => $metatag_object->title,
      'metatags[und][description][value]' => $metatag_object->description,
      'metatags[und][abstract][value]' => $metatag_object->abstract,
      'metatags[und][keywords][value]' => $metatag_object->keywords,
    );
    $this->drupalPost('admin/config/search/metatags/context/' . $metatag_object->name, $edit_metatag, t('Save'));
  }

  /**
   * Checks if metatags has been added correctly from a metatag_object instance.
   *
   * @return $metatag_object
   *   Metatag mapping object.
   */
  function checkMetatags($metatag_object) {
    $options = array('description', 'abstract', 'keywords');
    $this->drupalGet($metatag_object->path);

    foreach ($options as $option) {
      if (!empty($metatag_object->{$option})) {
        $this->assertRaw($metatag_object->{$option}, $option . ' found in ' . $metatag_object->path);
      }
      else {
        $this->assertNoRaw('<meta name="' . $option, $option . ' not found in ' . $metatag_object->path);
      }
    }
    if (!empty($metatag_object->title)) {
      $this->assertRaw($metatag_object->title, 'Title found in ' . $metatag_object->path);
    }
  }
}
