3 namespace Drupal\Tests\node\Traits;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\node\Entity\NodeType;
7 use PHPUnit\Framework\TestCase;
10 * Provides methods to create content type from given values.
12 * This trait is meant to be used only by test classes.
14 trait ContentTypeCreationTrait {
17 * Creates a custom content type based on default settings.
19 * @param array $values
20 * An array of settings to change from the defaults.
21 * Example: 'type' => 'foo'.
23 * @return \Drupal\node\Entity\NodeType
24 * Created content type.
26 protected function createContentType(array $values = []) {
27 // Find a non-existent random type name.
28 if (!isset($values['type'])) {
30 $id = strtolower($this->randomMachineName(8));
31 } while (NodeType::load($id));
34 $id = $values['type'];
40 $type = NodeType::create($values);
41 $status = $type->save();
42 node_add_body_field($type);
44 if ($this instanceof TestCase) {
45 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
48 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());