ContentTypeCreationTrait.php in Drupal 9
File
core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php
View source
<?php
namespace Drupal\Tests\node\Traits;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\node\Entity\NodeType;
use PHPUnit\Framework\TestCase;
trait ContentTypeCreationTrait {
protected function createContentType(array $values = []) {
if (!isset($values['type'])) {
do {
$id = strtolower($this
->randomMachineName(8));
} while (NodeType::load($id));
}
else {
$id = $values['type'];
}
$values += [
'type' => $id,
'name' => $id,
];
$type = NodeType::create($values);
$status = $type
->save();
node_add_body_field($type);
if ($this instanceof TestCase) {
$this
->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', [
'%type' => $type
->id(),
]))
->__toString());
}
else {
$this
->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created content type %type.', [
'%type' => $type
->id(),
]))
->__toString());
}
return $type;
}
}