metatag.tags_helper.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Base class for testing a module's custom tags.
  4. */
  5. abstract class MetatagTagsTestBase extends MetatagTestHelper {
  6. /**
  7. * All of the meta tags defined by this module which will be tested.
  8. */
  9. public $tags = array();
  10. /**
  11. * The tag to look for when testing the output.
  12. */
  13. public $test_tag = 'meta';
  14. /**
  15. * The attribute to look for to indicate which tag.
  16. */
  17. public $test_name_attribute = 'name';
  18. /**
  19. * The attribute to look for when testing the output.
  20. */
  21. public $test_value_attribute = 'content';
  22. /**
  23. * {@inheritdoc}
  24. */
  25. function setUp(array $modules = array()) {
  26. $modules[] = 'metatag_test';
  27. parent::setUp($modules);
  28. // Create an admin user that can manage meta tags.
  29. $account = $this->createAdminUser();
  30. $this->drupalLogin($account);
  31. }
  32. /**
  33. * Tests that this module's tags are available.
  34. */
  35. function testTagsArePresent() {
  36. // Load the global config.
  37. $this->drupalGet('admin/config/search/metatags/config/global');
  38. $this->assertResponse(200);
  39. // Confirm the various meta tags are available.
  40. foreach ($this->tags as $tag) {
  41. // Convert tag names to something more suitable for a function name.
  42. $tag_name = str_replace(array('.', '-', ':'), '_', $tag);
  43. // Look for a custom method named "{$tag_name}_test_field_xpath", if found
  44. // use that method to get the xpath definition for this meta tag,
  45. // otherwise it defaults to just looking for a text input field.
  46. $method = "{$tag_name}_test_field_xpath";
  47. if (method_exists($this, $method)) {
  48. $xpath = $this->$method();
  49. }
  50. else {
  51. $xpath = "//input[@name='metatags[und][{$tag}][value]' and @type='text']";
  52. }
  53. $this->assertFieldByXPath($xpath, NULL, format_string('The "%tag" tag field was found.', array('%tag' => $tag)));
  54. }
  55. }
  56. /**
  57. * Confirm that each tag can be saved and that the output of each tag is
  58. * correct.
  59. */
  60. function testTagsInputOutput() {
  61. // Load the global config.
  62. $this->drupalGet('admin/config/search/metatags/config/global');
  63. $this->assertResponse(200);
  64. // Update the Global defaults and test them.
  65. $all_values = $values = array();
  66. foreach ($this->tags as $tag_raw) {
  67. // Convert tag names to something more suitable for a function name.
  68. $tag_name = str_replace(array('.', '-', ':', ' '), '_', $tag_raw);
  69. // Look for a custom method named "{$tag_name}_test_key", if found use
  70. // that method to get the test string for this meta tag, otherwise it
  71. // defaults to the meta tag's name.
  72. $method = "{$tag_name}_test_key";
  73. if (method_exists($this, $method)) {
  74. $test_key = $this->$method();
  75. }
  76. else {
  77. $test_key = "metatags[und][{$tag_raw}][value]";
  78. }
  79. // Look for a custom method named "{$tag_name}_test_value", if found use
  80. // that method to get the test string for this meta tag, otherwise it
  81. // defaults to just generating a random string.
  82. $method = "{$tag_name}_test_value";
  83. if (method_exists($this, $method)) {
  84. $test_value = $this->$method();
  85. }
  86. else {
  87. // Generate a random string.
  88. $test_value = $this->getTestTagValue();
  89. }
  90. $values[$test_key] = $test_value;
  91. // Look for a custom method named "{$tag_name}_test_preprocess_output", if
  92. // found use that method provide any additional processing on the value
  93. // e.g. adding a prefix.
  94. $method = "{$tag_name}_test_preprocess_output";
  95. if (method_exists($this, $method)) {
  96. $test_value = $this->$method($test_value);
  97. }
  98. $all_values[$tag_name] = $test_value;
  99. }
  100. $this->drupalPost(NULL, $values, 'Save');
  101. $this->assertText(strip_tags(t('The meta tag defaults for @label have been saved.', array('@label' => 'Global'))));
  102. // Load the test page.
  103. $this->drupalGet('moosqueakoinkmeow');
  104. $this->assertResponse(200);
  105. $this->assertText(t('Test page.'));
  106. // Look for the values.
  107. foreach ($this->tags as $tag_raw) {
  108. // Convert tag names to something more suitable for a function name.
  109. $tag_name = str_replace(array('.', '-', ':', ' '), '_', $tag_raw);
  110. // Verify this meta tag was output.
  111. $this->assertTrue(!empty($all_values[$tag_name]));
  112. // Look for a custom method named "{$tag_name}_test_output_xpath", if
  113. // found use that method to get the xpath definition for this meta tag,
  114. // otherwise it defaults to just looking for a meta tag matching:
  115. // <$test_tag $test_name_attribute=$tag_name $test_value_attribute=$value />
  116. $method = "{$tag_name}_test_output_xpath";
  117. if (method_exists($this, $method)) {
  118. $xpath_string = $this->$method();
  119. }
  120. else {
  121. // Look for a custom method named "{$tag_name}_test_tag", if
  122. // found use that method to get the xpath definition for this meta tag,
  123. // otherwise it defaults to $this->test_tag.
  124. $method = "{$tag_name}_test_tag";
  125. if (method_exists($this, $method)) {
  126. $xpath_tag = $this->$method();
  127. }
  128. else {
  129. $xpath_tag = $this->test_tag;
  130. }
  131. // Look for a custom method named "{$tag_name}_test_name_attribute", if
  132. // found use that method to get the xpath definition for this meta tag,
  133. // otherwise it defaults to $this->test_name_attribute.
  134. $method = "{$tag_name}_test_name_attribute";
  135. if (method_exists($this, $method)) {
  136. $xpath_name_attribute = $this->$method();
  137. }
  138. else {
  139. $xpath_name_attribute = $this->test_name_attribute;
  140. }
  141. // Look for a custom method named "{$tag_name}_test_tag_name", if
  142. // found use that method to get the xpath definition for this meta tag,
  143. // otherwise it defaults to $tag_name.
  144. $method = "{$tag_name}_test_tag_name";
  145. if (method_exists($this, $method)) {
  146. $xpath_name_tag = $this->$method();
  147. }
  148. else {
  149. $xpath_name_tag = $this->getTestTagName($tag_name);
  150. }
  151. // Compile the xpath.
  152. $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
  153. }
  154. // Something should have been found.
  155. $this->assertTrue(!empty($xpath_string));
  156. // Look for a custom method named "{$tag_name}_test_value_attribute", if
  157. // found use that method to get the xpath definition for this meta tag,
  158. // otherwise it defaults to $this->test_value_attribute.
  159. $method = "{$tag_name}_test_value_attribute";
  160. if (method_exists($this, $method)) {
  161. $xpath_value_attribute = $this->$method();
  162. }
  163. else {
  164. $xpath_value_attribute = $this->test_value_attribute;
  165. }
  166. // Extract the meta tag from the HTML.
  167. $xpath = $this->xpath($xpath_string);
  168. $this->assertEqual(count($xpath), 1, format_string('One @name tag found.', array('@name' => $tag_name)));
  169. // Most meta tags have an attribute, but some don't.
  170. if (!empty($xpath_value_attribute)) {
  171. $this->assertTrue($xpath_value_attribute);
  172. $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
  173. // Help with debugging.
  174. if (!isset($xpath[0][$xpath_value_attribute])) {
  175. $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
  176. }
  177. else {
  178. if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
  179. $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
  180. }
  181. $this->assertTrue($xpath[0][$xpath_value_attribute]);
  182. $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name]);//, "The meta tag was found with the expected value.");
  183. }
  184. }
  185. else {
  186. $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
  187. $this->assertTrue((string)$xpath[0]);
  188. $this->assertEqual((string)$xpath[0], $all_values[$tag_name], "The meta tag was found with the expected value.");
  189. }
  190. }
  191. }
  192. /**
  193. * Convert a tag's internal name to the string which is actually used in HTML.
  194. *
  195. * The meta tag internal name will be machine names, i.e. only contain a-z,
  196. * A-Z, 0-9 and the underline character. Meta tag names will actually contain
  197. * any possible character.
  198. *
  199. * @param string $tag_name
  200. * The tag name to be converted.
  201. *
  202. * @return string
  203. * The converted tag name.
  204. */
  205. public function getTestTagName($tag_name) {
  206. return $tag_name;
  207. }
  208. /**
  209. * Generate a random value for testing meta tag fields.
  210. *
  211. * As a reasonable default, this will generating two words of 8 characters
  212. * each with simple machine name -style strings.
  213. *
  214. * @return string
  215. * A normal string.
  216. */
  217. public function getTestTagValue() {
  218. return $this->randomMachineName() . ' ' . $this->randomMachineName();
  219. }
  220. /**
  221. * Generate a URL for an image.
  222. *
  223. * @return string
  224. * An absolute URL to a non-existant image.
  225. */
  226. public function randomImageUrl() {
  227. return 'http://www.example.com/images/' . $this->randomMachineName() . '.png';
  228. }
  229. }