metatag.with_views.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Tests for the Metatag module for Views integration, outside of
  4. * Metatag:Views.
  5. */
  6. class MetatagCoreWithViewsTest extends MetatagTestHelper {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Metatag core tests with Views',
  13. 'description' => 'Test Metatag integration with the Views module.',
  14. 'group' => 'Metatag',
  15. 'dependencies' => array('ctools', 'token', 'views'),
  16. );
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. function setUp(array $modules = array()) {
  22. $modules[] = 'views';
  23. $modules[] = 'views_ui';
  24. // Need the Path module to set a alias which can then be loaded.
  25. $modules[] = 'path';
  26. parent::setUp($modules);
  27. }
  28. /**
  29. * Test the taxonomy term page still works when displayed via Views.
  30. */
  31. public function testTermViews() {
  32. $vocab_name = 'test_vocab';
  33. $term_name = 'Who likes magic';
  34. $term_path = 'term-test';
  35. // Create a vocabulary.
  36. $vocabulary = $this->createVocabulary($vocab_name);
  37. // Create an admin user and log them in.
  38. $perms = array(
  39. // Global admin access.
  40. 'administer site configuration',
  41. // Needed for the vocabulary.
  42. 'administer taxonomy',
  43. 'edit terms in ' . $vocabulary->vid,
  44. 'delete terms in ' . $vocabulary->vid,
  45. // Needed for the Path module.
  46. 'create url aliases',
  47. // Needed for Views.
  48. 'administer views',
  49. );
  50. $this->adminUser = $this->createAdminUser($perms);
  51. // Log in the admin user.
  52. $this->drupalLogin($this->adminUser);
  53. // Load the "add default configuration" page.
  54. $this->drupalGet('admin/config/search/metatags/config/add');
  55. $this->assertResponse(200);
  56. // Verify the page loaded correct.
  57. $this->assertText(t('Select the type of default meta tags you would like to add.'));
  58. // Submit the initial form to select an entity bundle.
  59. $this->drupalPost(NULL, array(
  60. 'instance' => 'taxonomy_term:' . $vocabulary->name,
  61. ), t('Add and configure'));
  62. $this->assertResponse(200);
  63. // @todo Update this to extract the H1 tag.
  64. $this->assertText(strip_tags('Taxonomy term: ' . $vocabulary->name));
  65. // Submit the form with some values.
  66. $this->drupalPost(NULL, array(
  67. 'metatags[und][abstract][value]' => '[term:name]',
  68. ), t('Save'));
  69. $this->assertResponse(200);
  70. // Verify the page loaded correct.
  71. $this->assertText(strip_tags(t('The meta tag defaults for @label have been saved.', array('@label' => 'Taxonomy term: ' . $vocabulary->name))));
  72. // Verify that the user was redirected to the settings page again.
  73. $this->assertEqual($this->getUrl(), url('admin/config/search/metatags', array('absolute' => TRUE)));
  74. // Create a test term.
  75. // Load the term form.
  76. $this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add');
  77. $this->assertResponse(200);
  78. // Verify the page loaded correctly.
  79. // @todo Update this to extract the H1 tag.
  80. // $this->assertText(strip_tags(t('Create @name', array('@name' => $vocabulary->name))));
  81. // Verify that it's possible to submit values to the form.
  82. $this->drupalPost(NULL, array(
  83. 'metatags[und][abstract][value]' => '[term:name] ponies',
  84. 'name' => $term_name,
  85. 'path[alias]' => $term_path,
  86. ), t('Save'));
  87. $this->assertResponse(200);
  88. // Verify that the node saved correctly.
  89. $t_args = array('%name' => $term_name);
  90. // This doesn't work for some reason, it seems the HTML is stripped off
  91. // during output so the %name's standard Drupal wrappers don't match.
  92. // $this->assertText(t('Created new term %name.', $t_args));
  93. // .. so this has to be done instead.
  94. $this->assertText(strip_tags(t('Created new term %name.', $t_args)));
  95. // Verify the term data saved correctly.
  96. $this->drupalGet($term_path);
  97. $this->assertResponse(200);
  98. // Confirm the page is managed by the default taxonomy term page.
  99. $this->assertText(t('There is currently no content classified with this term.'));
  100. // Enable the taxonomy_term default display.
  101. $view = views_get_view('taxonomy_term');
  102. ctools_include('export');
  103. ctools_export_set_object_status($view, 0);
  104. $this->clearAllCaches();
  105. $this->drupalGet('admin/structure/views');
  106. $this->assertResponse(200);
  107. $vars = variable_get('views_defaults');
  108. $this->verbose($vars);
  109. $this->assertFalse($vars['taxonomy_term'], 'Taxonomy term page is enabled.');
  110. // Load the page again.
  111. $this->drupalGet($term_path);
  112. $this->assertResponse(200);
  113. // Confirm this page is managed by Views - if it's managed by the default
  114. // taxonomy term page this text will show.
  115. $this->assertNoText(t('There is currently no content classified with this term.'));
  116. // Try to extract the 'edit' link.
  117. $xpaths = $this->xpath("//ul/li/a");
  118. $matches = array();
  119. $tid = 0;
  120. if (!empty($xpaths)) {
  121. foreach ($xpaths as $xpath) {
  122. $attributes = $xpath->attributes();
  123. if (!empty($attributes['href'])) {
  124. if (preg_match('@taxonomy/term/(\d+)/edit$@', $attributes['href'], $matches)) {
  125. $tid = $matches[1];
  126. }
  127. }
  128. }
  129. }
  130. // Presuing a term ID was found, load the term.
  131. if (!empty($tid)) {
  132. $term = taxonomy_term_load($tid);
  133. // Only the non-default values are stored.
  134. $expected = array(
  135. 'und' => array(
  136. 'abstract' => array(
  137. 'value' => '[term:name] ponies',
  138. ),
  139. ),
  140. );
  141. $this->assertEqual($expected, $term->metatags);
  142. }
  143. // This shouldn't happen, it indicates a problem.
  144. else {
  145. $this->fail(t('Could not determine the ID for created term.'));
  146. }
  147. // Verify the title is using the custom default for this vocabulary.
  148. $xpath = $this->xpath("//meta[@name='abstract']");
  149. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  150. $this->assertEqual($xpath[0]['content'], "Who likes magic ponies");
  151. }
  152. }