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