metatag.node.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * Tests for the Metatag module and node entities.
  4. */
  5. class MetatagCoreNodeTest extends MetatagTestHelper {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Metatag core tests for nodes',
  12. 'description' => 'Test Metatag edit functionality for nodes.',
  13. 'group' => 'Metatag',
  14. 'dependencies' => array('ctools', 'token'),
  15. );
  16. }
  17. /**
  18. * Tests creation of a standard entity.
  19. */
  20. public function testEntityCreationWorkflow() {
  21. $content_type = 'metatag_test';
  22. $content_type_path = str_replace('_', '-', $content_type);
  23. $label = 'Test';
  24. // Create a content type.
  25. $this->createContentType($content_type, $label);
  26. // Create an admin user and log them in.
  27. $perms = array(
  28. // Needed for the content type.
  29. 'create ' . $content_type . ' content',
  30. 'delete any ' . $content_type . ' content',
  31. 'edit any ' . $content_type . ' content',
  32. // Used later for revision handling.
  33. 'view revisions',
  34. 'revert revisions',
  35. 'delete revisions',
  36. // This permission is required in order to create new revisions.
  37. 'administer nodes',
  38. );
  39. $this->adminUser = $this->createAdminUser($perms);
  40. // Log in the admin user.
  41. $this->drupalLogin($this->adminUser);
  42. // Assign default values for the new content type.
  43. // Load the "add default configuration" page.
  44. $this->drupalGet('admin/config/search/metatags/config/add');
  45. $this->assertResponse(200);
  46. // Verify the page loaded correct.
  47. $this->assertText(t('Select the type of default meta tags you would like to add.'));
  48. // Submit the initial form to select an entity bundle.
  49. $this->drupalPost(NULL, array(
  50. 'instance' => 'node:' . $content_type,
  51. ), t('Add and configure'));
  52. $this->assertResponse(200);
  53. // Verify the page loaded correct.
  54. $this->assertText('Node: ' . $label);
  55. // Submit the form with some values.
  56. $this->drupalPost(NULL, array(
  57. 'metatags[und][abstract][value]' => '[node:title]',
  58. ), t('Save'));
  59. $this->assertResponse(200);
  60. // Verify the page loaded correct.
  61. $this->assertText(strip_tags(t('The meta tag defaults for @label have been saved.', array('@label' => 'Node: ' . $label))));
  62. // Verify that the user was redirected to the settings page again.
  63. $this->assertEqual($this->getUrl(), url('admin/config/search/metatags', array('absolute' => TRUE)));
  64. // Create a test node.
  65. // Load the node form.
  66. $this->drupalGet('node/add/' . $content_type_path);
  67. $this->assertResponse(200);
  68. // Verify the page loaded correctly.
  69. // @todo Update this to extract the page title.
  70. $this->assertText(strip_tags(t('Create @name', array('@name' => $label))));
  71. // Verify that it's possible to submit values to the form.
  72. $this->drupalPost(NULL, array(
  73. 'metatags[und][abstract][value]' => '[node:title] ponies',
  74. 'title' => 'Who likes magic',
  75. ), t('Save'));
  76. $this->assertResponse(200);
  77. // The meta tags that will be checked for.
  78. $expected = array(
  79. 'und' => array(
  80. 'abstract' => array(
  81. 'value' => '[node:title] ponies',
  82. ),
  83. ),
  84. );
  85. // Verify that the node saved correctly.
  86. // $xpath = $this->xpath("//h1");
  87. $t_args = array('@type' => 'Test', '%title' => 'Who likes magic');
  88. // This doesn't work for some reason, it seems the HTML is stripped off
  89. // during output so the %title's standard Drupal wrappers don't match.
  90. // $this->assertText(t('@type %title has been created.', $t_args));
  91. // .. so this has to be done instead.
  92. $this->assertText(strip_tags(t('@type %title has been created.', $t_args)));
  93. // Verify the node data saved correctly.
  94. $matches = array();
  95. $nid = 0;
  96. if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
  97. $nid = end($matches);
  98. $node = node_load($nid);
  99. $this->verbose($node, 'node_load(' . $nid . ')');
  100. // Only the non-default values are stored.
  101. $this->assertEqual($expected, $node->metatags);
  102. // Confirm the APIs can load the data for this node.
  103. $metatags = metatag_metatags_load('node', $node->nid);
  104. $this->verbose($metatags, 'metatag_metatags_load("node", ' . $node->nid . ')');
  105. $this->assertEqual($expected, $metatags);
  106. $metatags = metatag_metatags_load_multiple('node', array($node->nid));
  107. $this->verbose($metatags, 'metatag_metatags_load_multiple("node", array(' . $node->nid . '))');
  108. $this->assertEqual(array($node->nid => array($node->vid => $expected)), $metatags);
  109. // Confirm the APIs can load the data for this node revision.
  110. $metatags = metatag_metatags_load('node', $node->nid, $node->vid);
  111. $this->verbose($metatags, 'metatag_metatags_load("node", ' . $node->nid . ', ' . $node->vid . ')');
  112. $this->assertEqual($expected, $metatags);
  113. $metatags = metatag_metatags_load_multiple('node', array($node->nid), array($node->vid));
  114. $this->verbose($metatags, 'metatag_metatags_load_multiple("node", array(' . $node->nid . '), array(' . $node->vid . '))');
  115. $this->assertEqual(array($node->nid => array($node->vid => $expected)), $metatags);
  116. }
  117. // This shouldn't happen, it indicates a problem.
  118. else {
  119. $this->fail(t('Could not determine the ID for the created node.'));
  120. }
  121. // Verify the title is using the custom default for this content type.
  122. $xpath = $this->xpath("//meta[@name='abstract']");
  123. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  124. $this->assertEqual($xpath[0]['content'], 'Who likes magic ponies');
  125. // Core's canonical tag is a relative URL, whereas Metatag outputs an
  126. // absolute URL.
  127. $old_canonical = url('node/' . $node->nid);
  128. $new_canonical = url('node/' . $node->nid, array('absolute' => TRUE));
  129. // Confirm that the canonical tag is in the correct format.
  130. $xpath = $this->xpath("//link[@rel='canonical']");
  131. $this->assertEqual(count($xpath), 1, 'Exactly one canonical meta tag found.');
  132. $this->assertEqual($xpath[0]['href'], $new_canonical);
  133. $this->assertNotEqual($xpath[0]['href'], $old_canonical);
  134. // Try loading the node revisions page.
  135. $this->drupalGet('node/' . $node->nid . '/revisions');
  136. // Verify the page did not load correctly. This is because the revisions
  137. // page can't be loaded if there's only one revision.
  138. $this->assertResponse(403);
  139. // Try creating a revision of the node.
  140. $old_title = $node->title;
  141. $old_vid = $node->vid;
  142. $new_title = 'Who still likes magic';
  143. // Load the node-edit page.
  144. $this->drupalGet('node/' . $node->nid . '/edit');
  145. // Verify the page loaded correctly.
  146. $this->assertResponse(200);
  147. // Try submitting text to the page.
  148. $args = array(
  149. 'metatags[und][abstract][value]' => '[node:title] kittens',
  150. 'title' => $new_title,
  151. 'revision' => 1,
  152. 'log' => 'Creating a new revision',
  153. );
  154. $this->drupalPost(NULL, $args, t('Save'));
  155. // Verify the page submission loaded correctly.
  156. $this->assertResponse(200);
  157. // A new version of the expected results
  158. $expected_updated = array(
  159. 'und' => array(
  160. 'abstract' => array(
  161. 'value' => '[node:title] kittens',
  162. ),
  163. ),
  164. );
  165. // Verify that the node saved correctly.
  166. // $xpath = $this->xpath("//h1");
  167. $t_args = array('@type' => 'Test', '%title' => $new_title);
  168. // This doesn't work for some reason, it seems the HTML is stripped off
  169. // during output so the %title's standard Drupal wrappers don't match.
  170. // $this->assertText(t('@type %title has been updated.', $t_args));
  171. // .. so this has to be done instead.
  172. $this->assertText(strip_tags(t('@type %title has been updated.', $t_args)));
  173. // Verify the title is still using the custom default for this content type.
  174. $xpath = $this->xpath("//meta[@name='abstract']");
  175. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  176. $this->assertNotEqual($xpath[0]['content'], $old_title . ' ponies', 'Did not find the new abstract meta tag.');
  177. $this->assertEqual($xpath[0]['content'], $new_title . ' kittens', 'Found the old abstract meta tag.');
  178. // Load the node revisions page.
  179. $this->drupalGet('node/' . $node->nid . '/revisions');
  180. // Verify the page loaded correctly.
  181. $this->assertResponse(200, 'Loaded the revisions page for this node.');
  182. // Confirm there are two revisions.
  183. $xpath = $this->xpath("//body//div[@class='content']//table//tbody//tr");
  184. $this->assertEqual(count($xpath), 2, 'There are two revisions of this node.');
  185. // Load the previous revision.
  186. $this->drupalGet('node/' . $node->nid . '/revisions/' . $old_vid . '/view');
  187. // Verify the page loaded correctly.
  188. $this->assertResponse(200, 'Loaded the original revision of this node.');
  189. $xpath = $this->xpath("//meta[@name='abstract']");
  190. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  191. $this->assertNotEqual($xpath[0]['content'], $new_title . ' kittens', 'Did not find the new abstract meta tag.');
  192. $this->assertEqual($xpath[0]['content'], $old_title . ' ponies', 'Found the old abstract meta tag.');
  193. // Load the updated node; force-load it to make sure it's loaded properly.
  194. $updated_node = node_load($node->nid, NULL, TRUE);
  195. $updated_vid = $updated_node->vid;
  196. $this->verbose($updated_node, 'node_load(' . $node->nid . ', NULL, TRUE)');
  197. // Confirm the APIs can load the data for this node.
  198. $metatags = metatag_metatags_load('node', $updated_node->nid);
  199. $this->verbose($metatags, 'metatag_metatags_load("node", ' . $updated_node->nid . ')');
  200. $this->assertEqual($expected_updated, $metatags);
  201. $this->assertNotEqual($expected, $metatags);
  202. // This one is complicated. If only the entity id is passed in it will load
  203. // the {metatag} records for *all* of the entity's revisions.
  204. $metatags = metatag_metatags_load_multiple('node', array($updated_node->nid));
  205. $this->verbose($metatags, 'metatag_metatags_load_multiple("node", array(' . $updated_node->nid . '))');
  206. $this->assertEqual(array($updated_node->nid => array($node->vid => $expected, $updated_node->vid => $expected_updated)), $metatags);
  207. // Confirm the APIs can load the data for this node revision.
  208. $metatags = metatag_metatags_load('node', $updated_node->nid, $updated_vid);
  209. $this->verbose($metatags, 'metatag_metatags_load("node", ' . $updated_node->nid . ', ' . $updated_node->vid . ')');
  210. $this->assertEqual($expected_updated, $metatags);
  211. $this->assertNotEqual($expected, $metatags);
  212. $metatags = metatag_metatags_load_multiple('node', array($updated_node->nid), array($updated_node->vid));
  213. $this->verbose($metatags, 'metatag_metatags_load_multiple("node", array(' . $updated_node->nid . '), array(' . $updated_node->vid . '))');
  214. $this->assertEqual(array($updated_node->nid => array($updated_node->vid => $expected_updated)), $metatags);
  215. // Load the current revision again.
  216. $this->drupalGet('node/' . $node->nid);
  217. $this->assertResponse(200, 'Loaded the current revision of this node.');
  218. $xpath = $this->xpath("//meta[@name='abstract']");
  219. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  220. $this->assertNotEqual($xpath[0]['content'], $old_title . ' ponies', 'Did not find the old abstract meta tag.');
  221. $this->assertEqual($xpath[0]['content'], $new_title . ' kittens', 'Found the new abstract meta tag.');
  222. // Revert to the original revision.
  223. $this->drupalGet('node/' . $node->nid . '/revisions/' . $old_vid . '/revert');
  224. // Verify the page loaded correctly.
  225. $this->assertResponse(200, 'Loaded the form to revert to the original revision of this node.');
  226. // Try submitting the form.
  227. $this->drupalPost(NULL, array(), t('Revert'));
  228. // Verify the page submission loaded correctly.
  229. $this->assertResponse(200);
  230. // Confirm there are now three revisions.
  231. $xpath = $this->xpath("//body//div[@class='content']//table//tbody//tr");
  232. $this->assertEqual(count($xpath), 3, 'There are now three revisions of this node.');
  233. // Load the current revision, which will now have the old meta tags.
  234. $this->drupalGet('node/' . $node->nid);
  235. $this->assertResponse(200, 'Loaded the current revision of this node.');
  236. $xpath = $this->xpath("//meta[@name='abstract']");
  237. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  238. $this->assertNotEqual($xpath[0]['content'], $new_title . ' kittens', 'Did not find the new abstract meta tag.');
  239. $this->assertEqual($xpath[0]['content'], $old_title . ' ponies', 'Found the old abstract meta tag again.');
  240. // Delete the original revision.
  241. $this->drupalGet('node/' . $node->nid . '/revisions/' . $old_vid . '/delete');
  242. // Verify the page loaded correctly.
  243. $this->assertResponse(200, 'Loaded the form to delete the original revision of this node.');
  244. // Try submitting the form.
  245. $this->drupalPost(NULL, array(), t('Delete'));
  246. // Verify the page submission loaded correctly.
  247. $this->assertResponse(200);
  248. // Confirm there are now two revisions again.
  249. $xpath = $this->xpath("//body//div[@class='content']//table//tbody//tr");
  250. $this->assertEqual(count($xpath), 2, 'There are two revisions of this node again.');
  251. // Clear the caches and then load the current revision, just to confirm
  252. // that the page is still loading correctly.
  253. metatag_config_cache_clear();
  254. $this->drupalGet('node/' . $node->nid);
  255. $this->assertResponse(200, 'Loaded the current revision of this node again.');
  256. $xpath = $this->xpath("//meta[@name='abstract']");
  257. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  258. $this->assertNotEqual($xpath[0]['content'], $new_title . ' kittens', 'Did not find the new abstract meta tag.');
  259. $this->assertEqual($xpath[0]['content'], $old_title . ' ponies', 'Found the old abstract meta tag again.');
  260. // Delete the second revision.
  261. $this->drupalGet('node/' . $node->nid . '/revisions/' . $updated_vid . '/delete');
  262. // Verify the page loaded correctly.
  263. $this->assertResponse(200, 'Loaded the form to delete the second revision of this node.');
  264. // Try submitting the form.
  265. $this->drupalPost(NULL, array(), t('Delete'));
  266. $this->assertResponse(200);
  267. // Verify that the revisions page no longer loads because there's only one
  268. // revision now.
  269. $this->drupalGet('node/' . $node->nid . '/revisions');
  270. $this->assertResponse(403, 'No longer able to load the node revisions page.');
  271. // Clear the caches and then load the current revision, just to confirm
  272. // that the page is still loading correctly.
  273. metatag_config_cache_clear();
  274. $this->drupalGet('node/' . $node->nid);
  275. $this->assertResponse(200, 'Loaded the current revision of this node again.');
  276. $xpath = $this->xpath("//meta[@name='abstract']");
  277. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  278. $this->assertNotEqual($xpath[0]['content'], $new_title . ' kittens', 'Did not find the new abstract meta tag.');
  279. $this->assertEqual($xpath[0]['content'], $old_title . ' ponies', 'Found the old abstract meta tag again.');
  280. }
  281. /**
  282. * Tests different 'preview' options. #1: Disabled.
  283. */
  284. public function testNodePreviewOption0() {
  285. $this->checkNodePreviewOption(0);
  286. }
  287. /**
  288. * Tests different 'preview' options. #2: Optional, without preview.
  289. */
  290. public function testNodePreviewOption1NoPreview() {
  291. $this->checkNodePreviewOption(1, FALSE);
  292. }
  293. /**
  294. * Tests different 'preview' options. #2: Optional, with preview.
  295. */
  296. public function testNodePreviewOption1Preview() {
  297. $this->checkNodePreviewOption(1, TRUE);
  298. }
  299. /**
  300. * Tests different 'preview' options. #3: Preview required.
  301. */
  302. public function testNodePreviewOption2() {
  303. $this->checkNodePreviewOption(2);
  304. }
  305. /**
  306. * Change the node preview option at the content type level, confirm meta tags
  307. * still save correctly.
  308. *
  309. * @param int $option
  310. * A suitable value for the 'node_preview' option for a content type, must
  311. * be either 0, 1 or 2.
  312. * @param bool $preview
  313. * Whether to perform a preview. Has the following implications:
  314. * - if $option === 0, $preview is ignored, no preview is performed.
  315. * - if $option === 1, $preview is considered, a preview may be performed.
  316. * - if $option === 2, $preview is ignored, a preview is performed.
  317. */
  318. function checkNodePreviewOption($option, $preview = FALSE) {
  319. $content_type = 'article';
  320. $label = 'Test';
  321. // Create an admin user and log them in.
  322. $perms = array(
  323. // Needed for the content type.
  324. 'create ' . $content_type . ' content',
  325. 'edit any ' . $content_type . ' content',
  326. // Required for customizing the node preview option.
  327. 'administer content types',
  328. );
  329. $this->adminUser = $this->createAdminUser($perms);
  330. // Log in the admin user.
  331. $this->drupalLogin($this->adminUser);
  332. // Set the node preview mode.
  333. $this->drupalGet('admin/structure/types/manage/' . $content_type);
  334. $this->assertResponse(200);
  335. $edit = array(
  336. 'node_preview' => $option,
  337. );
  338. $this->drupalPost(NULL, $edit, t('Save content type'));
  339. $this->assertText(strip_tags(t('The content type %name has been updated.', array('%name' => t('Article')))));
  340. // Create a test node.
  341. $this->drupalGet('node/add/' . $content_type);
  342. $this->assertResponse(200);
  343. // Save a custom meta tag.
  344. $edit = array(
  345. 'metatags[und][abstract][value]' => '[node:title] ponies',
  346. 'title' => 'Who likes magic',
  347. );
  348. // A preview may be optionally performed. Core allows three combinations:
  349. // * 0 = Disallowed.
  350. // * 1 = Optional.
  351. // * 2 = Required.
  352. if (($option === 1 && $preview) || $option === 2) {
  353. $this->drupalPost(NULL, $edit, t('Preview'));
  354. $this->drupalPost(NULL, array(), t('Save'));
  355. }
  356. else {
  357. $this->drupalPost(NULL, $edit, t('Save'));
  358. }
  359. $this->assertResponse(200);
  360. // Verify the title is using the custom default for this content type.
  361. $xpath = $this->xpath("//meta[@name='abstract']");
  362. $this->assertEqual(count($xpath), 1, 'Exactly one abstract meta tag found.');
  363. $this->assertEqual($xpath[0]['content'], 'Who likes magic ponies');
  364. }
  365. }