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