annotate core/modules/node/tests/src/Traits/NodeCreationTrait.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children af1871eacc83
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@0 6
Chris@0 7 /**
Chris@0 8 * Provides methods to create node based on default settings.
Chris@0 9 *
Chris@0 10 * This trait is meant to be used only by test classes.
Chris@0 11 */
Chris@0 12 trait NodeCreationTrait {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Get a node from the database based on its title.
Chris@0 16 *
Chris@0 17 * @param string|\Drupal\Component\Render\MarkupInterface $title
Chris@0 18 * A node title, usually generated by $this->randomMachineName().
Chris@0 19 * @param $reset
Chris@0 20 * (optional) Whether to reset the entity cache.
Chris@0 21 *
Chris@0 22 * @return \Drupal\node\NodeInterface
Chris@0 23 * A node entity matching $title.
Chris@0 24 */
Chris@0 25 public function getNodeByTitle($title, $reset = FALSE) {
Chris@0 26 if ($reset) {
Chris@0 27 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
Chris@0 28 }
Chris@0 29 // Cast MarkupInterface objects to string.
Chris@0 30 $title = (string) $title;
Chris@0 31 $nodes = \Drupal::entityTypeManager()
Chris@0 32 ->getStorage('node')
Chris@0 33 ->loadByProperties(['title' => $title]);
Chris@0 34 // Load the first node returned from the database.
Chris@0 35 $returned_node = reset($nodes);
Chris@0 36 return $returned_node;
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Creates a node based on default settings.
Chris@0 41 *
Chris@0 42 * @param array $settings
Chris@0 43 * (optional) An associative array of settings for the node, as used in
Chris@0 44 * entity_create(). Override the defaults by specifying the key and value
Chris@0 45 * in the array, for example:
Chris@0 46 * @code
Chris@0 47 * $this->drupalCreateNode(array(
Chris@0 48 * 'title' => t('Hello, world!'),
Chris@0 49 * 'type' => 'article',
Chris@0 50 * ));
Chris@0 51 * @endcode
Chris@0 52 * The following defaults are provided:
Chris@0 53 * - body: Random string using the default filter format:
Chris@0 54 * @code
Chris@0 55 * $settings['body'][0] = array(
Chris@0 56 * 'value' => $this->randomMachineName(32),
Chris@0 57 * 'format' => filter_default_format(),
Chris@0 58 * );
Chris@0 59 * @endcode
Chris@0 60 * - title: Random string.
Chris@0 61 * - type: 'page'.
Chris@0 62 * - uid: The currently logged in user, or anonymous.
Chris@0 63 *
Chris@0 64 * @return \Drupal\node\NodeInterface
Chris@0 65 * The created node entity.
Chris@0 66 */
Chris@0 67 protected function createNode(array $settings = []) {
Chris@0 68 // Populate defaults array.
Chris@0 69 $settings += [
Chris@0 70 'body' => [
Chris@0 71 [
Chris@0 72 'value' => $this->randomMachineName(32),
Chris@0 73 'format' => filter_default_format(),
Chris@0 74 ],
Chris@0 75 ],
Chris@0 76 'title' => $this->randomMachineName(8),
Chris@0 77 'type' => 'page',
Chris@0 78 'uid' => \Drupal::currentUser()->id(),
Chris@0 79 ];
Chris@0 80 $node = Node::create($settings);
Chris@0 81 $node->save();
Chris@0 82
Chris@0 83 return $node;
Chris@0 84 }
Chris@0 85
Chris@0 86 }