3 namespace Drupal\simpletest;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\node\Entity\NodeType;
9 * Provides methods to create content type from given values.
11 * This trait is meant to be used only by test classes.
13 trait ContentTypeCreationTrait {
16 * Creates a custom content type based on default settings.
18 * @param array $values
19 * An array of settings to change from the defaults.
20 * Example: 'type' => 'foo'.
22 * @return \Drupal\node\Entity\NodeType
23 * Created content type.
25 protected function createContentType(array $values = []) {
26 // Find a non-existent random type name.
27 if (!isset($values['type'])) {
29 $id = strtolower($this->randomMachineName(8));
30 } while (NodeType::load($id));
33 $id = $values['type'];
39 $type = NodeType::create($values);
40 $status = $type->save();
41 node_add_body_field($type);
43 if ($this instanceof \PHPUnit_Framework_TestCase) {
44 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
47 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());