annotate core/modules/node/tests/src/Traits/NodeCreationTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\node\Traits;
Chris@0 4
Chris@0 5 use Drupal\node\Entity\Node;
Chris@18 6 use Drupal\user\Entity\User;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Provides methods to create node based on default settings.
Chris@0 10 *
Chris@0 11 * This trait is meant to be used only by test classes.
Chris@0 12 */
Chris@0 13 trait NodeCreationTrait {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Get a node from the database based on its title.
Chris@0 17 *
Chris@0 18 * @param string|\Drupal\Component\Render\MarkupInterface $title
Chris@0 19 * A node title, usually generated by $this->randomMachineName().
Chris@0 20 * @param $reset
Chris@0 21 * (optional) Whether to reset the entity cache.
Chris@0 22 *
Chris@0 23 * @return \Drupal\node\NodeInterface
Chris@0 24 * A node entity matching $title.
Chris@0 25 */
Chris@0 26 public function getNodeByTitle($title, $reset = FALSE) {
Chris@0 27 if ($reset) {
Chris@0 28 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
Chris@0 29 }
Chris@0 30 // Cast MarkupInterface objects to string.
Chris@0 31 $title = (string) $title;
Chris@0 32 $nodes = \Drupal::entityTypeManager()
Chris@0 33 ->getStorage('node')
Chris@0 34 ->loadByProperties(['title' => $title]);
Chris@0 35 // Load the first node returned from the database.
Chris@0 36 $returned_node = reset($nodes);
Chris@0 37 return $returned_node;
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Creates a node based on default settings.
Chris@0 42 *
Chris@0 43 * @param array $settings
Chris@0 44 * (optional) An associative array of settings for the node, as used in
Chris@0 45 * entity_create(). Override the defaults by specifying the key and value
Chris@0 46 * in the array, for example:
Chris@0 47 * @code
Chris@0 48 * $this->drupalCreateNode(array(
Chris@0 49 * 'title' => t('Hello, world!'),
Chris@0 50 * 'type' => 'article',
Chris@0 51 * ));
Chris@0 52 * @endcode
Chris@0 53 * The following defaults are provided:
Chris@0 54 * - body: Random string using the default filter format:
Chris@0 55 * @code
Chris@0 56 * $settings['body'][0] = array(
Chris@0 57 * 'value' => $this->randomMachineName(32),
Chris@0 58 * 'format' => filter_default_format(),
Chris@0 59 * );
Chris@0 60 * @endcode
Chris@0 61 * - title: Random string.
Chris@0 62 * - type: 'page'.
Chris@0 63 * - uid: The currently logged in user, or anonymous.
Chris@0 64 *
Chris@0 65 * @return \Drupal\node\NodeInterface
Chris@0 66 * The created node entity.
Chris@0 67 */
Chris@0 68 protected function createNode(array $settings = []) {
Chris@0 69 // Populate defaults array.
Chris@0 70 $settings += [
Chris@0 71 'body' => [
Chris@0 72 [
Chris@0 73 'value' => $this->randomMachineName(32),
Chris@0 74 'format' => filter_default_format(),
Chris@0 75 ],
Chris@0 76 ],
Chris@0 77 'title' => $this->randomMachineName(8),
Chris@0 78 'type' => 'page',
Chris@0 79 ];
Chris@18 80
Chris@18 81 if (!array_key_exists('uid', $settings)) {
Chris@18 82 $user = User::load(\Drupal::currentUser()->id());
Chris@18 83 if ($user) {
Chris@18 84 $settings['uid'] = $user->id();
Chris@18 85 }
Chris@18 86 elseif (method_exists($this, 'setUpCurrentUser')) {
Chris@18 87 /** @var \Drupal\user\UserInterface $user */
Chris@18 88 $user = $this->setUpCurrentUser();
Chris@18 89 $settings['uid'] = $user->id();
Chris@18 90 }
Chris@18 91 else {
Chris@18 92 $settings['uid'] = 0;
Chris@18 93 }
Chris@18 94 }
Chris@18 95
Chris@0 96 $node = Node::create($settings);
Chris@0 97 $node->save();
Chris@0 98
Chris@0 99 return $node;
Chris@0 100 }
Chris@0 101
Chris@0 102 }