Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / node / tests / src / Traits / ContentTypeCreationTrait.php
1 <?php
2
3 namespace Drupal\Tests\node\Traits;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\node\Entity\NodeType;
7 use PHPUnit\Framework\TestCase;
8
9 /**
10  * Provides methods to create content type from given values.
11  *
12  * This trait is meant to be used only by test classes.
13  */
14 trait ContentTypeCreationTrait {
15
16   /**
17    * Creates a custom content type based on default settings.
18    *
19    * @param array $values
20    *   An array of settings to change from the defaults.
21    *   Example: 'type' => 'foo'.
22    *
23    * @return \Drupal\node\Entity\NodeType
24    *   Created content type.
25    */
26   protected function createContentType(array $values = []) {
27     // Find a non-existent random type name.
28     if (!isset($values['type'])) {
29       do {
30         $id = strtolower($this->randomMachineName(8));
31       } while (NodeType::load($id));
32     }
33     else {
34       $id = $values['type'];
35     }
36     $values += [
37       'type' => $id,
38       'name' => $id,
39     ];
40     $type = NodeType::create($values);
41     $status = $type->save();
42     node_add_body_field($type);
43
44     if ($this instanceof TestCase) {
45       $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
46     }
47     else {
48       $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
49     }
50
51     return $type;
52   }
53
54 }