358f8a17a4f821ec8109662f339472a1a974a11f
[yaffs-website] / web / modules / contrib / ctools / src / Testing / EntityCreationTrait.php
1 <?php
2
3 namespace Drupal\ctools\Testing;
4
5
6 use Drupal\Component\Render\FormattableMarkup;
7
8 trait EntityCreationTrait {
9
10   /**
11    * The entity type manager.
12    *
13    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
14    */
15   protected $entityTypeManager;
16
17   /**
18    * Creates a custom content type based on default settings.
19    *
20    * @param string $entity_type
21    *   The type of entity to create.
22    * @param array $values
23    *   An array of settings to change from the defaults.
24    *   Example: 'type' => 'foo'.
25    *
26    * @return \Drupal\Core\Entity\EntityInterface
27    *   Created entity.
28    */
29   protected function createEntity($entity_type, array $values = array()) {
30     $storage = $this->getEntityTypeManager()->getStorage($entity_type);
31     $entity = $storage->create($values);
32     $status = $entity->save();
33     \Drupal::service('router.builder')->rebuild();
34
35     if ($this instanceof \PHPUnit_Framework_TestCase) {
36       $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created entity %id of type %type.', ['%id' => $entity->id(), '%type' => $entity_type]))->__toString());
37     }
38     else {
39       $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created entity %id of type %type.', ['%id' => $entity->id(), '%type' => $entity_type]))->__toString());
40     }
41
42     return $entity;
43   }
44
45   /**
46    * @return \Drupal\Core\Entity\EntityTypeManagerInterface
47    */
48   protected function getEntityTypeManager() {
49     if (!isset($this->entityTypeManager)) {
50       $this->entityTypeManager = $this->container->get('entity_type.manager');
51     }
52     return $this->entityTypeManager;
53   }
54
55 }