diff --git a/core/modules/simpletest/src/BlockCreationTrait.php b/core/modules/simpletest/src/BlockCreationTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..446d842d1e07f419623d1c59f5767838d58d86e5 --- /dev/null +++ b/core/modules/simpletest/src/BlockCreationTrait.php @@ -0,0 +1,72 @@ +<?php + +/** + * @file + * Contains \Drupal\simpletest\BlockCreationTrait. + */ + +namespace Drupal\simpletest; + +use Drupal\block\Entity\Block; + +/** + * Provides methods to create and place block with default settings. + * + * This trait is meant to be used only by test classes. + */ +trait BlockCreationTrait { + + /** + * Creates a block instance based on default settings. + * + * @param string $plugin_id + * The plugin ID of the block type for this block instance. + * @param array $settings + * (optional) An associative array of settings for the block entity. + * Override the defaults by specifying the key and value in the array, for + * example: + * @code + * $this->drupalPlaceBlock('system_powered_by_block', array( + * 'label' => t('Hello, world!'), + * )); + * @endcode + * The following defaults are provided: + * - label: Random string. + * - ID: Random string. + * - region: 'sidebar_first'. + * - theme: The default theme. + * - visibility: Empty array. + * + * @return \Drupal\block\Entity\Block + * The block entity. + * + * @todo + * Add support for creating custom block instances. + */ + protected function placeBlock($plugin_id, array $settings = array()) { + $config = \Drupal::configFactory(); + $settings += array( + 'plugin' => $plugin_id, + 'region' => 'sidebar_first', + 'id' => strtolower($this->randomMachineName(8)), + 'theme' => $config->get('system.theme')->get('default'), + 'label' => $this->randomMachineName(8), + 'visibility' => array(), + 'weight' => 0, + ); + $values = []; + foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) { + $values[$key] = $settings[$key]; + // Remove extra values that do not belong in the settings array. + unset($settings[$key]); + } + foreach ($values['visibility'] as $id => $visibility) { + $values['visibility'][$id]['id'] = $id; + } + $values['settings'] = $settings; + $block = Block::create($values); + $block->save(); + return $block; + } + +} diff --git a/core/modules/simpletest/src/ContentTypeCreationTrait.php b/core/modules/simpletest/src/ContentTypeCreationTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..8152ab6f082a607518afed0f092dd7e0dbf38ae8 --- /dev/null +++ b/core/modules/simpletest/src/ContentTypeCreationTrait.php @@ -0,0 +1,58 @@ +<?php +/** + * @file + * Contains Drupal\simpletest\ContentTypeCreationTrait. + */ + +namespace Drupal\simpletest; + +use Drupal\Component\Render\FormattableMarkup; +use Drupal\node\Entity\NodeType; + +/** + * Provides methods to create content type from given values. + * + * This trait is meant to be used only by test classes. + */ +trait ContentTypeCreationTrait { + + /** + * Creates a custom content type based on default settings. + * + * @param array $values + * An array of settings to change from the defaults. + * Example: 'type' => 'foo'. + * + * @return \Drupal\node\Entity\NodeType + * Created content type. + */ + protected function createContentType(array $values = array()) { + // Find a non-existent random type name. + if (!isset($values['type'])) { + do { + $id = strtolower($this->randomMachineName(8)); + } while (NodeType::load($id)); + } + else { + $id = $values['type']; + } + $values += array( + 'type' => $id, + 'name' => $id, + ); + $type = NodeType::create($values); + $status = $type->save(); + node_add_body_field($type); + \Drupal::service('router.builder')->rebuild(); + + if ($this instanceof \PHPUnit_Framework_TestCase) { + $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', array('%type' => $type->id())))->__toString()); + } + else { + $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', array('%type' => $type->id())))->__toString()); + } + + return $type; + } + +} diff --git a/core/modules/simpletest/src/NodeCreationTrait.php b/core/modules/simpletest/src/NodeCreationTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..204af9ed8cdbc2d28c1c0bbc19f43d63adfa631b --- /dev/null +++ b/core/modules/simpletest/src/NodeCreationTrait.php @@ -0,0 +1,88 @@ +<?php +/** + * @file + * Contains Drupal\simpletest\NodeCreationTrait + */ + +namespace Drupal\simpletest; + +use Drupal\node\Entity\Node; + +/** + * Provides methods to create node based on default settings. + * + * This trait is meant to be used only by test classes. + */ +trait NodeCreationTrait { + + /** + * Get a node from the database based on its title. + * + * @param string|\Drupal\Component\Render\MarkupInterface $title + * A node title, usually generated by $this->randomMachineName(). + * @param $reset + * (optional) Whether to reset the entity cache. + * + * @return \Drupal\node\NodeInterface + * A node entity matching $title. + */ + function getNodeByTitle($title, $reset = FALSE) { + if ($reset) { + \Drupal::entityTypeManager()->getStorage('node')->resetCache(); + } + // Cast MarkupInterface objects to string. + $title = (string) $title; + $nodes = \Drupal::entityTypeManager() + ->getStorage('node') + ->loadByProperties(['title' => $title]); + // Load the first node returned from the database. + $returned_node = reset($nodes); + return $returned_node; + } + + /** + * Creates a node based on default settings. + * + * @param array $settings + * (optional) An associative array of settings for the node, as used in + * entity_create(). Override the defaults by specifying the key and value + * in the array, for example: + * @code + * $this->drupalCreateNode(array( + * 'title' => t('Hello, world!'), + * 'type' => 'article', + * )); + * @endcode + * The following defaults are provided: + * - body: Random string using the default filter format: + * @code + * $settings['body'][0] = array( + * 'value' => $this->randomMachineName(32), + * 'format' => filter_default_format(), + * ); + * @endcode + * - title: Random string. + * - type: 'page'. + * - uid: The currently logged in user, or anonymous. + * + * @return \Drupal\node\NodeInterface + * The created node entity. + */ + protected function createNode(array $settings = array()) { + // Populate defaults array. + $settings += array( + 'body' => array(array( + 'value' => $this->randomMachineName(32), + 'format' => filter_default_format(), + )), + 'title' => $this->randomMachineName(8), + 'type' => 'page', + 'uid' => \Drupal::currentUser()->id(), + ); + $node = Node::create($settings); + $node->save(); + + return $node; + } + +} diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 43bdf65dcdd73e45ccde4d106dd63043ae042f6b..c5fba7f2d4fe3599b61dbf3bf1e750e2a49fb9ef 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -29,7 +29,6 @@ use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Url; -use Drupal\node\Entity\NodeType; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Zend\Diactoros\Uri; @@ -42,7 +41,16 @@ abstract class WebTestBase extends TestBase { use AssertContentTrait; - + use BlockCreationTrait { + placeBlock as drupalPlaceBlock; + } + use ContentTypeCreationTrait { + createContentType as drupalCreateContentType; + } + use NodeCreationTrait { + getNodeByTitle as drupalGetNodeByTitle; + createNode as drupalCreateNode; + } use UserCreationTrait { createUser as drupalCreateUser; createRole as drupalCreateRole; @@ -230,108 +238,6 @@ function __construct($test_id = NULL) { $this->classLoader = require DRUPAL_ROOT . '/autoload.php'; } - /** - * Get a node from the database based on its title. - * - * @param string|\Drupal\Component\Render\MarkupInterface $title - * A node title, usually generated by $this->randomMachineName(). - * @param $reset - * (optional) Whether to reset the entity cache. - * - * @return \Drupal\node\NodeInterface - * A node entity matching $title. - */ - function drupalGetNodeByTitle($title, $reset = FALSE) { - if ($reset) { - \Drupal::entityManager()->getStorage('node')->resetCache(); - } - // Cast MarkupInterface objects to string. - $title = (string) $title; - $nodes = entity_load_multiple_by_properties('node', array('title' => $title)); - // Load the first node returned from the database. - $returned_node = reset($nodes); - return $returned_node; - } - - /** - * Creates a node based on default settings. - * - * @param array $settings - * (optional) An associative array of settings for the node, as used in - * entity_create(). Override the defaults by specifying the key and value - * in the array, for example: - * @code - * $this->drupalCreateNode(array( - * 'title' => t('Hello, world!'), - * 'type' => 'article', - * )); - * @endcode - * The following defaults are provided: - * - body: Random string using the default filter format: - * @code - * $settings['body'][0] = array( - * 'value' => $this->randomMachineName(32), - * 'format' => filter_default_format(), - * ); - * @endcode - * - title: Random string. - * - type: 'page'. - * - uid: The currently logged in user, or anonymous. - * - * @return \Drupal\node\NodeInterface - * The created node entity. - */ - protected function drupalCreateNode(array $settings = array()) { - // Populate defaults array. - $settings += array( - 'body' => array(array( - 'value' => $this->randomMachineName(32), - 'format' => filter_default_format(), - )), - 'title' => $this->randomMachineName(8), - 'type' => 'page', - 'uid' => \Drupal::currentUser()->id(), - ); - $node = entity_create('node', $settings); - $node->save(); - - return $node; - } - - /** - * Creates a custom content type based on default settings. - * - * @param array $values - * An array of settings to change from the defaults. - * Example: 'type' => 'foo'. - * - * @return \Drupal\node\Entity\NodeType - * Created content type. - */ - protected function drupalCreateContentType(array $values = array()) { - // Find a non-existent random type name. - if (!isset($values['type'])) { - do { - $id = strtolower($this->randomMachineName(8)); - } while (NodeType::load($id)); - } - else { - $id = $values['type']; - } - $values += array( - 'type' => $id, - 'name' => $id, - ); - $type = entity_create('node_type', $values); - $status = $type->save(); - node_add_body_field($type); - \Drupal::service('router.builder')->rebuild(); - - $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created content type %type.', array('%type' => $type->id()))); - - return $type; - } - /** * Builds the renderable view of an entity. * @@ -390,58 +296,6 @@ protected function drupalBuildEntityView(EntityInterface $entity, $view_mode = ' return $build; } - /** - * Creates a block instance based on default settings. - * - * @param string $plugin_id - * The plugin ID of the block type for this block instance. - * @param array $settings - * (optional) An associative array of settings for the block entity. - * Override the defaults by specifying the key and value in the array, for - * example: - * @code - * $this->drupalPlaceBlock('system_powered_by_block', array( - * 'label' => t('Hello, world!'), - * )); - * @endcode - * The following defaults are provided: - * - label: Random string. - * - ID: Random string. - * - region: 'sidebar_first'. - * - theme: The default theme. - * - visibility: Empty array. - * - * @return \Drupal\block\Entity\Block - * The block entity. - * - * @todo - * Add support for creating custom block instances. - */ - protected function drupalPlaceBlock($plugin_id, array $settings = array()) { - $settings += array( - 'plugin' => $plugin_id, - 'region' => 'sidebar_first', - 'id' => strtolower($this->randomMachineName(8)), - 'theme' => $this->config('system.theme')->get('default'), - 'label' => $this->randomMachineName(8), - 'visibility' => array(), - 'weight' => 0, - ); - $values = []; - foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) { - $values[$key] = $settings[$key]; - // Remove extra values that do not belong in the settings array. - unset($settings[$key]); - } - foreach ($values['visibility'] as $id => $visibility) { - $values['visibility'][$id]['id'] = $id; - } - $values['settings'] = $settings; - $block = entity_create('block', $values); - $block->save(); - return $block; - } - /** * Checks to see whether a block appears on the page. *