metatag.tags_helper.test 9.0 KB

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