metatag.term.test 5.3 KB

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