metatag.term.test 5.3 KB

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