Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentTestBase.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Sets up block content types.
11  */
12 abstract class BlockContentTestBase extends BrowserTestBase {
13
14   /**
15    * Profile to use.
16    */
17   protected $profile = 'testing';
18
19   /**
20    * Admin user
21    *
22    * @var \Drupal\user\UserInterface
23    */
24   protected $adminUser;
25
26   /**
27    * Permissions to grant admin user.
28    *
29    * @var array
30    */
31   protected $permissions = [
32     'administer blocks'
33   ];
34
35   /**
36    * Modules to enable.
37    *
38    * @var array
39    */
40   public static $modules = ['block', 'block_content'];
41
42   /**
43    * Whether or not to auto-create the basic block type during setup.
44    *
45    * @var bool
46    */
47   protected $autoCreateBasicBlockType = TRUE;
48
49   /**
50    * Sets the test up.
51    */
52   protected function setUp() {
53     parent::setUp();
54     if ($this->autoCreateBasicBlockType) {
55       $this->createBlockContentType('basic', TRUE);
56     }
57
58     $this->adminUser = $this->drupalCreateUser($this->permissions);
59     $this->drupalPlaceBlock('local_actions_block');
60   }
61
62   /**
63    * Creates a custom block.
64    *
65    * @param bool|string $title
66    *   (optional) Title of block. When no value is given uses a random name.
67    *   Defaults to FALSE.
68    * @param string $bundle
69    *   (optional) Bundle name. Defaults to 'basic'.
70    * @param bool $save
71    *   (optional) Whether to save the block. Defaults to TRUE.
72    *
73    * @return \Drupal\block_content\Entity\BlockContent
74    *   Created custom block.
75    */
76   protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
77     $title = $title ?: $this->randomMachineName();
78     $block_content = BlockContent::create([
79       'info' => $title,
80       'type' => $bundle,
81       'langcode' => 'en'
82     ]);
83     if ($block_content && $save === TRUE) {
84       $block_content->save();
85     }
86     return $block_content;
87   }
88
89   /**
90    * Creates a custom block type (bundle).
91    *
92    * @param string $label
93    *   The block type label.
94    * @param bool $create_body
95    *   Whether or not to create the body field
96    *
97    * @return \Drupal\block_content\Entity\BlockContentType
98    *   Created custom block type.
99    */
100   protected function createBlockContentType($label, $create_body = FALSE) {
101     $bundle = BlockContentType::create([
102       'id' => $label,
103       'label' => $label,
104       'revision' => FALSE,
105     ]);
106     $bundle->save();
107     if ($create_body) {
108       block_content_add_body_field($bundle->id());
109     }
110     return $bundle;
111   }
112
113 }