page_title.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @file
  4. * Test cases for the Page Title module.
  5. */
  6. class PageTitleTestCase extends DrupalWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Page Title',
  10. 'description' => 'Ensure that Page Title functions correctly',
  11. 'group' => 'Page Title',
  12. 'dependencies' => array('token'),
  13. );
  14. }
  15. public function setUp() {
  16. parent::setUp('token', 'page_title', 'forum', 'taxonomy');
  17. // Create an admin user
  18. $this->admin_user = $this->drupalCreateUser(array(
  19. 'administer page titles',
  20. 'administer taxonomy',
  21. 'access content',
  22. 'create page content',
  23. 'edit any page content',
  24. 'administer forums',
  25. 'create forum content',
  26. 'set page title')
  27. );
  28. }
  29. private function pageTitleCheck($path, $pattern, $tokens = array(), $section = '') {
  30. // Get the path...
  31. if (is_array($path)) {
  32. $this->drupalGet($path['path'], $path['options']);
  33. }
  34. else {
  35. $this->drupalGet($path);
  36. }
  37. // Apply tokens to pattern
  38. $title = strtr($pattern, $tokens);
  39. // Do some verbose output
  40. $this->pass(t('Pattern: %pat', array('%pat' => '<title>' . $pattern . '</title>')));
  41. $this->pass(t('Looking for title: %title', array('%title' => '<title>' . $title . '</title>')));
  42. // Look for the title in the content
  43. $res = $this->assertRaw($title, t('Correct page title found in the <head> for: %section', array('%section' => $section)), 'Page Title');
  44. // If the search failed, do some more verbose debugging
  45. if (!$res) {
  46. preg_match('/<title>.*<\/title>/', $this->drupalGetContent(), $found_title);
  47. $this->pass(t('Found instead: %found', array('%found' => $found_title[0])));
  48. }
  49. }
  50. public function testPageTitleTest() {
  51. $this->drupalLogin($this->admin_user);
  52. // Create a term
  53. $term = array(
  54. 'name' => 'Test Term Foo',
  55. );
  56. $this->drupalPost('admin/structure/taxonomy/tags/add', $term, t('Save'));
  57. // Define our settings
  58. $settings = array(
  59. 'page_title_default' => '[current-page:page-title] - DEFAULT TEST',
  60. 'page_title_front' => '[site:name]',
  61. 'page_title_user' => 'Profile For [user]',
  62. 'page_title_type_page' => 'PAGE NODE: [current-page:page-title]',
  63. 'page_title_type_page_showfield' => 1,
  64. 'page_title_type_forum' => 'Forum - [current-page:page-title]',
  65. 'page_title_pager_pattern' => ' - page [current-page:page-number]',
  66. 'page_title_vocab_forums' => 'FORUM: [term:name]',
  67. 'page_title_vocab_tags' => 'TERM: [current-page:page-title]',
  68. 'page_title_vocab_forums_showfield' => 1,
  69. 'page_title_vocab_tags_showfield' => 1,
  70. 'page_title_forum_root_title' => 'Welcome to [site:name] [current-page:page-title]',
  71. );
  72. // Save the settings
  73. $this->drupalPost('admin/config/search/page-title', $settings, t('Save configuration'));
  74. $this->assertText(t('The configuration options have been saved.'), t('The configuration saved message was found'), 'Page Title');
  75. /**
  76. * Lets check the frontpage page title is working
  77. */
  78. $this->pageTitleCheck('<front>', $settings['page_title_front'], array('[site:name]' => 'Drupal'), 'Frontpage Title');
  79. /**
  80. * Lets check a "default" page, such a the page title admin form
  81. */
  82. $this->pageTitleCheck('admin/config/search/page-title', $settings['page_title_default'], array('[current-page:page-title]' => 'Page titles'), 'Admin Page');
  83. /**
  84. * Let's create a page node and check that
  85. */
  86. //Create a basic page node
  87. $node = array(
  88. 'type' => 'page',
  89. 'title' => 'Test Page Node',
  90. 'taxonomy' => array(2 => 1), // Set taxonomy for vocab 2 to term 1 See Hack/assumption above
  91. );
  92. // Save the node
  93. $node = $this->drupalCreateNode($node);
  94. // Pass out a message to confirm the save
  95. $this->pass(t('Created Node !nid', array('!nid' => $node->nid)), 'Page Title');
  96. // Load the node page and check for the title in the head
  97. $this->pageTitleCheck('node/' . $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->title), 'Page Node Type');
  98. // Post a page_title into the node and reload the node
  99. $edit['page_title'] = 'I am a test Page Title field';
  100. $this->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  101. $node = node_load($node->nid, NULL, TRUE);
  102. // Node load the node page and check for the title in the head
  103. $this->pageTitleCheck('node/' . $node->nid, $settings['page_title_type_page'], array('[current-page:page-title]' => $node->page_title), 'Page Node Type');
  104. /**
  105. * TAXONOMY
  106. */
  107. // Lets check a taxonomy/term/tid page (should be term from earlier!)
  108. // Load the term page and check for the title in the head
  109. $this->pageTitleCheck('taxonomy/term/2', $settings['page_title_vocab_tags'], array('[current-page:page-title]' => $term['name']), 'Taxonomy Title');
  110. // Lets check the pagenation suffix is working but appending it to taxonomy/term/1.
  111. // This is a little messy - but it works for our purpose
  112. // Remember, the page value in the URL is zero indexed. This means page=1 in URL is Page 2 on the site
  113. $this->pageTitleCheck(
  114. array('path' => 'taxonomy/term/2', 'options' => array('query' => array('page' => 1))),
  115. $settings['page_title_vocab_tags'] . $settings['page_title_pager_pattern'],
  116. array('[current-page:page-title]' => $term['name'], '[current-page:page-number]' => 2),
  117. 'Taxonomy Title with pagenation suffix'
  118. );
  119. /**
  120. * FORUMS
  121. */
  122. // Updating term 1 - The General Discussion forum
  123. #$forum_forum = array('page_title' => 'I AM A TEST FORUM');
  124. #$this->drupalPost('admin/structure/forum/edit/forum/1', $forum_forum, t('Save'));
  125. //Create a basic forum topic node
  126. $forum_node = array(
  127. 'nid' => NULL,
  128. 'type' => 'forum',
  129. 'title' => 'Test Forum Node',
  130. '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
  131. );
  132. // Save the node
  133. $forum_node = $this->drupalCreateNode($forum_node);
  134. // Node load the node page and check for the title in the head
  135. $this->pageTitleCheck('node/' . $forum_node->nid, $settings['page_title_type_forum'], array('[current-page:page-title]' => $forum_node->title), 'Forum Topic');
  136. //Now test the forum root...
  137. $this->pageTitleCheck('forum', $settings['page_title_forum_root_title'], array('[current-page:page-title]' => 'Forums', '[site:name]' => 'Drupal'), 'Forum Topic');
  138. //Now test the forum forum...
  139. $this->pageTitleCheck('forum/1', $settings['page_title_vocab_forums'], array('[term:name]' => 'General discussion'), 'Forum Container');
  140. }
  141. }