PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/node/src/Tests/PagePreviewTest.php

https://gitlab.com/reasonat/test8
PHP | 341 lines | 224 code | 51 blank | 66 comment | 0 complexity | e82a57455c3b08c55cc8acec28c4d457 MD5 | raw file
  1. <?php
  2. namespace Drupal\node\Tests;
  3. use Drupal\comment\Tests\CommentTestTrait;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\Component\Utility\Unicode;
  6. use Drupal\Core\Language\LanguageInterface;
  7. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  8. use Drupal\field\Entity\FieldConfig;
  9. use Drupal\field\Entity\FieldStorageConfig;
  10. use Drupal\node\Entity\NodeType;
  11. use Drupal\taxonomy\Entity\Term;
  12. use Drupal\taxonomy\Entity\Vocabulary;
  13. /**
  14. * Tests the node entity preview functionality.
  15. *
  16. * @group node
  17. */
  18. class PagePreviewTest extends NodeTestBase {
  19. use EntityReferenceTestTrait;
  20. use CommentTestTrait;
  21. /**
  22. * Enable the comment, node and taxonomy modules to test on the preview.
  23. *
  24. * @var array
  25. */
  26. public static $modules = array('node', 'taxonomy', 'comment', 'image', 'file');
  27. /**
  28. * The name of the created field.
  29. *
  30. * @var string
  31. */
  32. protected $fieldName;
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->addDefaultCommentField('node', 'page');
  36. $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
  37. $this->drupalLogin($web_user);
  38. // Add a vocabulary so we can test different view modes.
  39. $vocabulary = Vocabulary::create([
  40. 'name' => $this->randomMachineName(),
  41. 'description' => $this->randomMachineName(),
  42. 'vid' => $this->randomMachineName(),
  43. 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  44. 'help' => '',
  45. ]);
  46. $vocabulary->save();
  47. $this->vocabulary = $vocabulary;
  48. // Add a term to the vocabulary.
  49. $term = Term::create([
  50. 'name' => $this->randomMachineName(),
  51. 'description' => $this->randomMachineName(),
  52. 'vid' => $this->vocabulary->id(),
  53. 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  54. ]);
  55. $term->save();
  56. $this->term = $term;
  57. // Create an image field.
  58. FieldStorageConfig::create([
  59. 'field_name' => 'field_image',
  60. 'entity_type' => 'node',
  61. 'type' => 'image',
  62. 'settings' => [],
  63. 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
  64. ])->save();
  65. $field_config = FieldConfig::create([
  66. 'field_name' => 'field_image',
  67. 'label' => 'Images',
  68. 'entity_type' => 'node',
  69. 'bundle' => 'page',
  70. 'required' => FALSE,
  71. 'settings' => [],
  72. ]);
  73. $field_config->save();
  74. // Create a field.
  75. $this->fieldName = Unicode::strtolower($this->randomMachineName());
  76. $handler_settings = array(
  77. 'target_bundles' => array(
  78. $this->vocabulary->id() => $this->vocabulary->id(),
  79. ),
  80. 'auto_create' => TRUE,
  81. );
  82. $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  83. entity_get_form_display('node', 'page', 'default')
  84. ->setComponent($this->fieldName, array(
  85. 'type' => 'entity_reference_autocomplete_tags',
  86. ))
  87. ->save();
  88. // Show on default display and teaser.
  89. entity_get_display('node', 'page', 'default')
  90. ->setComponent($this->fieldName, array(
  91. 'type' => 'entity_reference_label',
  92. ))
  93. ->save();
  94. entity_get_display('node', 'page', 'teaser')
  95. ->setComponent($this->fieldName, array(
  96. 'type' => 'entity_reference_label',
  97. ))
  98. ->save();
  99. entity_get_form_display('node', 'page', 'default')
  100. ->setComponent('field_image', array(
  101. 'type' => 'image_image',
  102. 'settings' => [],
  103. ))
  104. ->save();
  105. entity_get_display('node', 'page', 'default')
  106. ->setComponent('field_image')
  107. ->save();
  108. }
  109. /**
  110. * Checks the node preview functionality.
  111. */
  112. function testPagePreview() {
  113. $title_key = 'title[0][value]';
  114. $body_key = 'body[0][value]';
  115. $term_key = $this->fieldName . '[target_id]';
  116. // Fill in node creation form and preview node.
  117. $edit = array();
  118. $edit[$title_key] = '<em>' . $this->randomMachineName(8) . '</em>';
  119. $edit[$body_key] = $this->randomMachineName(16);
  120. $edit[$term_key] = $this->term->getName();
  121. // Upload an image.
  122. $test_image = current($this->drupalGetTestFiles('image', 39325));
  123. $edit['files[field_image_0][]'] = drupal_realpath($test_image->uri);
  124. $this->drupalPostForm('node/add/page', $edit, t('Upload'));
  125. // Add an alt tag and preview the node.
  126. $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));
  127. // Check that the preview is displaying the title, body and term.
  128. $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.');
  129. $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
  130. $this->assertText($edit[$body_key], 'Body displayed.');
  131. $this->assertText($edit[$term_key], 'Term displayed.');
  132. $this->assertLink(t('Back to content editing'));
  133. // Get the UUID.
  134. $url = parse_url($this->getUrl());
  135. $paths = explode('/', $url['path']);
  136. $view_mode = array_pop($paths);
  137. $uuid = array_pop($paths);
  138. // Switch view mode. We'll remove the body from the teaser view mode.
  139. entity_get_display('node', 'page', 'teaser')
  140. ->removeComponent('body')
  141. ->save();
  142. $view_mode_edit = array('view_mode' => 'teaser');
  143. $this->drupalPostForm('node/preview/' . $uuid . '/default', $view_mode_edit, t('Switch'));
  144. $this->assertRaw('view-mode-teaser', 'View mode teaser class found.');
  145. $this->assertNoText($edit[$body_key], 'Body not displayed.');
  146. // Check that the title, body and term fields are displayed with the
  147. // values after going back to the content edit page.
  148. $this->clickLink(t('Back to content editing'));
  149. $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
  150. $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
  151. $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
  152. $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas');
  153. // Return to page preview to check everything is as expected.
  154. $this->drupalPostForm(NULL, array(), t('Preview'));
  155. $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.');
  156. $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
  157. $this->assertText($edit[$body_key], 'Body displayed.');
  158. $this->assertText($edit[$term_key], 'Term displayed.');
  159. $this->assertLink(t('Back to content editing'));
  160. // Assert the content is kept when reloading the page.
  161. $this->drupalGet('node/add/page', array('query' => array('uuid' => $uuid)));
  162. $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
  163. $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
  164. $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
  165. // Save the node - this is a new POST, so we need to upload the image.
  166. $this->drupalPostForm('node/add/page', $edit, t('Upload'));
  167. $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Save'));
  168. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  169. // Check the term was displayed on the saved node.
  170. $this->drupalGet('node/' . $node->id());
  171. $this->assertText($edit[$term_key], 'Term displayed.');
  172. // Check the term appears again on the edit form.
  173. $this->drupalGet('node/' . $node->id() . '/edit');
  174. $this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
  175. // Check with two new terms on the edit form, additionally to the existing
  176. // one.
  177. $edit = array();
  178. $newterm1 = $this->randomMachineName(8);
  179. $newterm2 = $this->randomMachineName(8);
  180. $edit[$term_key] = $this->term->getName() . ', ' . $newterm1 . ', ' . $newterm2;
  181. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
  182. $this->assertRaw('>' . $newterm1 . '<', 'First new term displayed.');
  183. $this->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.');
  184. // The first term should be displayed as link, the others not.
  185. $this->assertLink($this->term->getName());
  186. $this->assertNoLink($newterm1);
  187. $this->assertNoLink($newterm2);
  188. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
  189. // Check with one more new term, keeping old terms, removing the existing
  190. // one.
  191. $edit = array();
  192. $newterm3 = $this->randomMachineName(8);
  193. $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2;
  194. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
  195. $this->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.');
  196. $this->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.');
  197. $this->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.');
  198. $this->assertNoText($this->term->getName());
  199. $this->assertLink($newterm1);
  200. $this->assertLink($newterm2);
  201. $this->assertNoLink($newterm3);
  202. // Check that editing an existing node after it has been previewed and not
  203. // saved doesn't remember the previous changes.
  204. $edit = array(
  205. $title_key => $this->randomMachineName(8),
  206. );
  207. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
  208. $this->assertText($edit[$title_key], 'New title displayed.');
  209. $this->clickLink(t('Back to content editing'));
  210. $this->assertFieldByName($title_key, $edit[$title_key], 'New title value displayed.');
  211. // Navigate away from the node without saving.
  212. $this->drupalGet('<front>');
  213. // Go back to the edit form, the title should have its initial value.
  214. $this->drupalGet('node/' . $node->id() . '/edit');
  215. $this->assertFieldByName($title_key, $node->label(), 'Correct title value displayed.');
  216. // Check with required preview.
  217. $node_type = NodeType::load('page');
  218. $node_type->setPreviewMode(DRUPAL_REQUIRED);
  219. $node_type->save();
  220. $this->drupalGet('node/add/page');
  221. $this->assertNoRaw('edit-submit');
  222. $this->drupalPostForm('node/add/page', array($title_key => 'Preview'), t('Preview'));
  223. $this->clickLink(t('Back to content editing'));
  224. $this->assertRaw('edit-submit');
  225. }
  226. /**
  227. * Checks the node preview functionality, when using revisions.
  228. */
  229. function testPagePreviewWithRevisions() {
  230. $title_key = 'title[0][value]';
  231. $body_key = 'body[0][value]';
  232. $term_key = $this->fieldName . '[target_id]';
  233. // Force revision on "Basic page" content.
  234. $node_type = NodeType::load('page');
  235. $node_type->setNewRevision(TRUE);
  236. $node_type->save();
  237. // Fill in node creation form and preview node.
  238. $edit = array();
  239. $edit[$title_key] = $this->randomMachineName(8);
  240. $edit[$body_key] = $this->randomMachineName(16);
  241. $edit[$term_key] = $this->term->id();
  242. $edit['revision_log[0][value]'] = $this->randomString(32);
  243. $this->drupalPostForm('node/add/page', $edit, t('Preview'));
  244. // Check that the preview is displaying the title, body and term.
  245. $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.');
  246. $this->assertText($edit[$title_key], 'Title displayed.');
  247. $this->assertText($edit[$body_key], 'Body displayed.');
  248. $this->assertText($edit[$term_key], 'Term displayed.');
  249. // Check that the title and body fields are displayed with the correct
  250. // values after going back to the content edit page.
  251. $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
  252. $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
  253. $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
  254. // Check that the revision log field has the correct value.
  255. $this->assertFieldByName('revision_log[0][value]', $edit['revision_log[0][value]'], 'Revision log field displayed.');
  256. // Save the node after coming back from the preview page so we can create a
  257. // forward revision for it.
  258. $this->drupalPostForm(NULL, [], t('Save'));
  259. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  260. // Check that previewing a forward revision of a node works. This can not be
  261. // accomplished through the UI so we have to use API calls.
  262. // @todo Change this test to use the UI when we will be able to create
  263. // forward revisions in core.
  264. // @see https://www.drupal.org/node/2725533
  265. $node->setNewRevision(TRUE);
  266. $node->isDefaultRevision(FALSE);
  267. /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */
  268. $controller_resolver = \Drupal::service('controller_resolver');
  269. $node_preview_controller = $controller_resolver->getControllerFromDefinition('\Drupal\node\Controller\NodePreviewController::view');
  270. $node_preview_controller($node, 'default');
  271. }
  272. /**
  273. * Checks the node preview accessible for simultaneous node editing.
  274. */
  275. public function testSimultaneousPreview() {
  276. $title_key = 'title[0][value]';
  277. $node = $this->drupalCreateNode(array());
  278. $edit = array($title_key => 'New page title');
  279. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
  280. $this->assertText($edit[$title_key]);
  281. $user2 = $this->drupalCreateUser(array('edit any page content'));
  282. $this->drupalLogin($user2);
  283. $this->drupalGet('node/' . $node->id() . '/edit');
  284. $this->assertFieldByName($title_key, $node->label(), 'No title leaked from previous user.');
  285. $edit2 = array($title_key => 'Another page title');
  286. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit2, t('Preview'));
  287. $this->assertUrl(\Drupal::url('entity.node.preview', ['node_preview' => $node->uuid(), 'view_mode_id' => 'default'], ['absolute' => TRUE]));
  288. $this->assertText($edit2[$title_key]);
  289. }
  290. }