metatag_context.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @file
  4. * Functional tests for the Metatag:Context module.
  5. */
  6. class MetatagContextTestCase extends DrupalWebTestCase {
  7. /**
  8. * The getInfo() method provides information about the test.
  9. * In order for the test to be run, the getInfo() method needs
  10. * to be implemented.
  11. */
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Meta tag context tests',
  15. 'description' => 'Test basic meta tag context functionality.',
  16. 'group' => 'Metatag',
  17. );
  18. }
  19. /**
  20. * Prepares the testing environment
  21. */
  22. public function setUp(array $modules = array()) {
  23. $modules[] = 'metatag';
  24. $modules[] = 'metatag_context';
  25. parent::setUp($modules);
  26. // Create user.
  27. $this->privileged_user = $this->drupalCreateUser(array(
  28. 'bypass node access',
  29. 'administer content types',
  30. 'administer meta tags',
  31. ));
  32. $this->drupalLogin($this->privileged_user);
  33. // Create content type, with underscores.
  34. $type_name = strtolower($this->randomName(8)) . '_test';
  35. $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
  36. $this->type = $type->type;
  37. // Store a valid URL name, with hyphens instead of underscores.
  38. $this->hyphen_type = str_replace('_', '-', $this->type);
  39. }
  40. /**
  41. * Performs the basic tests.
  42. */
  43. public function testMetatagContextBasic() {
  44. // Create content type node.
  45. $this->drupalPost('node/add/' . $this->hyphen_type, array('title' => $this->randomName(8)), t('Save'));
  46. $this->context_name = drupal_strtolower($this->randomName(8));
  47. // Generate metatags and check content.
  48. $this->metatag_pages['node'] = $this->createMetatagObject('node/1', 'node_metatags');
  49. $this->metatag_pages['page'] = $this->createMetatagObject('<front>', 'frontpage_metatags');
  50. foreach ($this->metatag_pages as $page) {
  51. $this->generateMetatag($page);
  52. $this->checkMetatags($page);
  53. }
  54. // Edit metatag and check content.
  55. $this->metatag_pages['node']->title = 'New title';
  56. $this->metatag_pages['node']->description = '';
  57. $this->editMetatag($this->metatag_pages['node']);
  58. $this->checkMetatags($this->metatag_pages['node']);
  59. }
  60. /**
  61. * Creates a metatag object which can be used for generate and check
  62. * the metatag_context module behavior.
  63. *
  64. * @param $path
  65. * Path where generate metatags.
  66. * @param $identifier
  67. * Custom test to identify metatags in source code.
  68. *
  69. * @return $metatag_object
  70. * Metatag mapping object.
  71. */
  72. function createMetatagObject($path, $identifier) {
  73. $metatag_object = new stdClass();
  74. $metatag_object->name = drupal_strtolower($this->randomName(10));
  75. $metatag_object->path = $path;
  76. $metatag_object->title = "My $identifier title";
  77. $metatag_object->description = "My $identifier description";
  78. $metatag_object->abstract = "My $identifier abstract";
  79. $metatag_object->keywords = "My $identifier keywords";
  80. return $metatag_object;
  81. }
  82. /**
  83. * Generates metatags by path from a metatag_object instance.
  84. *
  85. * @return $metatag_object
  86. * Metatag mapping object.
  87. */
  88. function generateMetatag($metatag_object) {
  89. //Add new Metatag object by path.
  90. $edit = array(
  91. 'name' => $metatag_object->name,
  92. );
  93. $this->drupalPost('admin/config/search/metatags/context/add', $edit, t('Add and configure'));
  94. $this->editMetatag($metatag_object);
  95. }
  96. /**
  97. * Edits metatags by path from a metatag_object instance.
  98. *
  99. * @return $metatag_object
  100. * Metatag mapping object.
  101. */
  102. function editMetatag($metatag_object) {
  103. $edit_metatag = array(
  104. 'paths' => $metatag_object->path,
  105. 'metatags[und][title][value]' => $metatag_object->title,
  106. 'metatags[und][description][value]' => $metatag_object->description,
  107. 'metatags[und][abstract][value]' => $metatag_object->abstract,
  108. 'metatags[und][keywords][value]' => $metatag_object->keywords,
  109. );
  110. $this->drupalPost('admin/config/search/metatags/context/' . $metatag_object->name, $edit_metatag, t('Save'));
  111. }
  112. /**
  113. * Checks if metatags has been added correctly from a metatag_object instance.
  114. *
  115. * @return $metatag_object
  116. * Metatag mapping object.
  117. */
  118. function checkMetatags($metatag_object) {
  119. $options = array('description', 'abstract', 'keywords');
  120. $this->drupalGet($metatag_object->path);
  121. foreach ($options as $option) {
  122. if (!empty($metatag_object->{$option})) {
  123. $this->assertRaw($metatag_object->{$option}, $option . ' found in ' . $metatag_object->path);
  124. }
  125. else {
  126. $this->assertNoRaw('<meta name="' . $option, $option . ' not found in ' . $metatag_object->path);
  127. }
  128. }
  129. if (!empty($metatag_object->title)) {
  130. $this->assertRaw($metatag_object->title, 'Title found in ' . $metatag_object->path);
  131. }
  132. }
  133. }