<?php

/**
 * @file
 * Test cases for the Page Title module.
 */

class PageTitleTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Page Title',
      'description' => 'Ensure that Page Title functions correctly',
      'group' => 'Page Title',
      'dependencies' => array('token'),
    );
  }

  public function setUp() {
    parent::setUp('token', 'page_title', 'forum', 'taxonomy');

    // Create an admin user
    $this->admin_user = $this->drupalCreateUser(array(
      'administer page titles',
      'administer taxonomy',
      'access content',
      'create page content',
      'edit any page content',
      'administer forums',
      'create forum content',
      'set page title')
    );
  }


  private function pageTitleCheck($path, $pattern, $tokens = array(), $section = '') {
    // Get the path...
    if (is_array($path)) {
      $this->drupalGet($path['path'], $path['options']);
    }
    else {
      $this->drupalGet($path);
    }

    // Apply tokens to pattern
    $title = strtr($pattern, $tokens);

    // Do some verbose output
    $this->pass(t('Pattern: %pat', array('%pat' => '<title>' . $pattern . '</title>')));
    $this->pass(t('Looking for title: %title', array('%title' => '<title>' . $title . '</title>')));

    // Look for the title in the content
    $res = $this->assertRaw($title, t('Correct page title found in the <head> for: %section', array('%section' => $section)), 'Page Title');

    // If the search failed, do some more verbose debugging
    if (!$res) {
      preg_match('/<title>.*<\/title>/', $this->drupalGetContent(), $found_title);
      $this->pass(t('Found instead: %found', array('%found' => $found_title[0])));
    }
  }


  public function testPageTitleTest() {
    $this->drupalLogin($this->admin_user);

    // Create a term
    $term = array(
      'name' => 'Test Term Foo',
    );
    $this->drupalPost('admin/structure/taxonomy/tags/add', $term, t('Save'));




    // Define our settings
    $settings = array(
      'page_title_default' => '[current-page:page-title] - DEFAULT TEST',
      'page_title_front' => '[site:name]',
      'page_title_user' => 'Profile For [user]',
      'page_title_type_page' => 'PAGE NODE: [current-page:page-title]',
      'page_title_type_page_showfield' => 1,
      'page_title_type_forum' => 'Forum - [current-page:page-title]',
      'page_title_pager_pattern' => ' - page [current-page:page-number]',
      'page_title_vocab_forums' => 'FORUM: [term:name]',
      'page_title_vocab_tags' => 'TERM: [current-page:page-title]',
      'page_title_vocab_forums_showfield' => 1,
      'page_title_vocab_tags_showfield' => 1,
      'page_title_forum_root_title' => 'Welcome to [site:name] [current-page:page-title]',
    );

    // Save the settings
    $this->drupalPost('admin/config/search/page-title', $settings, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved.'), t('The configuration saved message was found'), 'Page Title');





    /**
     * Lets check the frontpage page title is working
     */
    $this->pageTitleCheck('<front>', $settings['page_title_front'], array('[site:name]' => 'Drupal'), 'Frontpage Title');



    /**
     * Lets check a "default" page, such a the page title admin form
     */
    $this->pageTitleCheck('admin/config/search/page-title', $settings['page_title_default'], array('[current-page:page-title]' => 'Page titles'), 'Admin Page');



    /**
     * Let's create a page node and check that
     */

    //Create a basic page node
    $node = array(
      'type' => 'page',
      'title' => 'Test Page Node',
      'taxonomy' => array(2 => 1), // Set taxonomy for vocab 2 to term 1 See Hack/assumption above
    );

    // Save the node
    $node = $this->drupalCreateNode($node);

    // Pass out a message to confirm the save
    $this->pass(t('Created Node !nid', array('!nid' => $node->nid)), 'Page Title');

    // Load the node page and check for the title in the head
    $this->pageTitleCheck('node/' . $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->title), 'Page Node Type');

    // Post a page_title into the node and reload the node
    $edit['page_title'] = 'I am a test Page Title field';
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
    $node = node_load($node->nid, NULL, TRUE);

    // Node load the node page and check for the title in the head
    $this->pageTitleCheck('node/' . $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->page_title), 'Page Node Type');



    /**
     *  TAXONOMY
     */

    // Lets check a taxonomy/term/tid page (should be term from earlier!)
    // Load the term page and check for the title in the head
    $this->pageTitleCheck('taxonomy/term/2', $settings['page_title_vocab_tags'], array('[current-page:page-title]' => $term['name']), 'Taxonomy Title');



    // Lets check the pagenation suffix is working but appending it to taxonomy/term/1.
    // This is a little messy - but it works for our purpose
    // Remember, the page value in the URL is zero indexed. This means page=1 in URL is Page 2 on the site
    $this->pageTitleCheck(
      array('path' => 'taxonomy/term/2', 'options' => array('query' => array('page' => 1))),
      $settings['page_title_vocab_tags'] . $settings['page_title_pager_pattern'],
      array('[current-page:page-title]' => $term['name'], '[current-page:page-number]' => 2),
      'Taxonomy Title with pagenation suffix'
    );



    /**
     *  FORUMS
     */
    // Updating term 1 - The General Discussion forum
    #$forum_forum = array('page_title' => 'I AM A TEST FORUM');
    #$this->drupalPost('admin/structure/forum/edit/forum/1', $forum_forum, t('Save'));

    //Create a basic forum topic node
    $forum_node = array(
      'nid' => NULL,
      'type' => 'forum',
      'title' => 'Test Forum Node',
      'taxonomy_forums' => array('und' => array(0 => array('tid' => 1))), // Set taxonomy for vocab 1 (forum vocab) to term 1 (the General discussion forum)... See Hack/assumptions above
    );

    // Save the node
    $forum_node = $this->drupalCreateNode($forum_node);


    // Node load the node page and check for the title in the head
    $this->pageTitleCheck('node/' . $forum_node->nid, $settings['page_title_type_forum'], array('[current-page:page-title]' => $forum_node->title), 'Forum Topic');


    //Now test the forum root...
    $this->pageTitleCheck('forum', $settings['page_title_forum_root_title'], array('[current-page:page-title]' => 'Forums', '[site:name]' => 'Drupal'), 'Forum Topic');

    //Now test the forum forum...
    $this->pageTitleCheck('forum/1', $settings['page_title_vocab_forums'], array('[term:name]' => 'General discussion'), 'Forum Container');
  }
}
