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