PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php

http://github.com/drupal/drupal
PHP | 206 lines | 136 code | 36 blank | 34 comment | 0 complexity | a39e4b0fc6ef8490ee1675d2c46fb838 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Tests\hal\Kernel;
  3. use Drupal\comment\Tests\CommentTestTrait;
  4. use Drupal\comment\Entity\Comment;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\user\Entity\User;
  7. use Drupal\node\Entity\NodeType;
  8. use Drupal\taxonomy\Entity\Term;
  9. use Drupal\taxonomy\Entity\Vocabulary;
  10. /**
  11. * Tests that nodes and terms are correctly normalized and denormalized.
  12. *
  13. * @group hal
  14. */
  15. class EntityNormalizeTest extends NormalizerTestBase {
  16. use CommentTestTrait;
  17. /**
  18. * Modules to enable.
  19. *
  20. * @var array
  21. */
  22. public static $modules = array('node', 'taxonomy', 'comment');
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. \Drupal::service('router.builder')->rebuild();
  29. $this->installSchema('system', array('sequences'));
  30. $this->installSchema('comment', array('comment_entity_statistics'));
  31. $this->installEntitySchema('taxonomy_term');
  32. $this->installConfig(['node', 'comment']);
  33. }
  34. /**
  35. * Tests the normalization of nodes.
  36. */
  37. public function testNode() {
  38. $node_type = NodeType::create(['type' => 'example_type']);
  39. $node_type->save();
  40. $user = User::create(['name' => $this->randomMachineName()]);
  41. $user->save();
  42. // Add comment type.
  43. $this->container->get('entity.manager')->getStorage('comment_type')->create(array(
  44. 'id' => 'comment',
  45. 'label' => 'comment',
  46. 'target_entity_type_id' => 'node',
  47. ))->save();
  48. $this->addDefaultCommentField('node', 'example_type');
  49. $node = Node::create([
  50. 'title' => $this->randomMachineName(),
  51. 'uid' => $user->id(),
  52. 'type' => $node_type->id(),
  53. 'status' => NODE_PUBLISHED,
  54. 'promote' => 1,
  55. 'sticky' => 0,
  56. 'body' => [
  57. 'value' => $this->randomMachineName(),
  58. 'format' => $this->randomMachineName()
  59. ],
  60. 'revision_log' => $this->randomString(),
  61. ]);
  62. $node->save();
  63. $original_values = $node->toArray();
  64. $normalized = $this->serializer->normalize($node, $this->format);
  65. /** @var \Drupal\node\NodeInterface $denormalized_node */
  66. $denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format);
  67. $this->assertEqual($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.');
  68. }
  69. /**
  70. * Tests the normalization of terms.
  71. */
  72. public function testTerm() {
  73. $vocabulary = Vocabulary::create(['vid' => 'example_vocabulary']);
  74. $vocabulary->save();
  75. $account = User::create(['name' => $this->randomMachineName()]);
  76. $account->save();
  77. // @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is
  78. // set, the test fails because target_id => 0 is reserialized to NULL.
  79. $term_parent = Term::create([
  80. 'name' => $this->randomMachineName(),
  81. 'vid' => $vocabulary->id(),
  82. ]);
  83. $term_parent->save();
  84. $term = Term::create([
  85. 'name' => $this->randomMachineName(),
  86. 'vid' => $vocabulary->id(),
  87. 'description' => array(
  88. 'value' => $this->randomMachineName(),
  89. 'format' => $this->randomMachineName(),
  90. ),
  91. 'parent' => $term_parent->id(),
  92. ]);
  93. $term->save();
  94. $original_values = $term->toArray();
  95. $normalized = $this->serializer->normalize($term, $this->format, ['account' => $account]);
  96. /** @var \Drupal\taxonomy\TermInterface $denormalized_term */
  97. $denormalized_term = $this->serializer->denormalize($normalized, 'Drupal\taxonomy\Entity\Term', $this->format, ['account' => $account]);
  98. $this->assertEqual($original_values, $denormalized_term->toArray(), 'Term values are restored after normalizing and denormalizing.');
  99. }
  100. /**
  101. * Tests the normalization of comments.
  102. */
  103. public function testComment() {
  104. $node_type = NodeType::create(['type' => 'example_type']);
  105. $node_type->save();
  106. $account = User::create(['name' => $this->randomMachineName()]);
  107. $account->save();
  108. // Add comment type.
  109. $this->container->get('entity.manager')->getStorage('comment_type')->create(array(
  110. 'id' => 'comment',
  111. 'label' => 'comment',
  112. 'target_entity_type_id' => 'node',
  113. ))->save();
  114. $this->addDefaultCommentField('node', 'example_type');
  115. $node = Node::create([
  116. 'title' => $this->randomMachineName(),
  117. 'uid' => $account->id(),
  118. 'type' => $node_type->id(),
  119. 'status' => NODE_PUBLISHED,
  120. 'promote' => 1,
  121. 'sticky' => 0,
  122. 'body' => [[
  123. 'value' => $this->randomMachineName(),
  124. 'format' => $this->randomMachineName()
  125. ]],
  126. ]);
  127. $node->save();
  128. $parent_comment = Comment::create(array(
  129. 'uid' => $account->id(),
  130. 'subject' => $this->randomMachineName(),
  131. 'comment_body' => [
  132. 'value' => $this->randomMachineName(),
  133. 'format' => NULL,
  134. ],
  135. 'entity_id' => $node->id(),
  136. 'entity_type' => 'node',
  137. 'field_name' => 'comment',
  138. ));
  139. $parent_comment->save();
  140. $comment = Comment::create(array(
  141. 'uid' => $account->id(),
  142. 'subject' => $this->randomMachineName(),
  143. 'comment_body' => [
  144. 'value' => $this->randomMachineName(),
  145. 'format' => NULL,
  146. ],
  147. 'entity_id' => $node->id(),
  148. 'entity_type' => 'node',
  149. 'field_name' => 'comment',
  150. 'pid' => $parent_comment->id(),
  151. 'mail' => 'dries@drupal.org',
  152. 'homepage' => 'http://buytaert.net',
  153. ));
  154. $comment->save();
  155. $original_values = $comment->toArray();
  156. // Hostname will always be denied view access.
  157. // No value will exist for name as this is only for anonymous users.
  158. unset($original_values['hostname'], $original_values['name']);
  159. $normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);
  160. // Assert that the hostname field does not appear at all in the normalized
  161. // data.
  162. $this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.');
  163. /** @var \Drupal\comment\CommentInterface $denormalized_comment */
  164. $denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format, ['account' => $account]);
  165. // Before comparing, unset values that are expected to differ.
  166. $denormalized_comment_values = $denormalized_comment->toArray();
  167. unset($denormalized_comment_values['hostname'], $denormalized_comment_values['name']);
  168. $this->assertEqual($original_values, $denormalized_comment_values, 'The expected comment values are restored after normalizing and denormalizing.');
  169. }
  170. }