Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / src / Tests / Views / BlockContentTestBase.php
1 <?php
2
3 namespace Drupal\block_content\Tests\Views;
4
5 @trigger_error('\Drupal\block_content\Tests\Views\BlockContentTestBase is deprecated in 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\block_content\Functional\Views\BlockContentTestBase.', E_USER_DEPRECATED);
6
7 use Drupal\block_content\Entity\BlockContent;
8 use Drupal\block_content\Entity\BlockContentType;
9 use Drupal\Component\Utility\SafeMarkup;
10 use Drupal\views\Tests\ViewTestBase;
11 use Drupal\views\Tests\ViewTestData;
12
13 /**
14  * Base class for all block_content tests.
15  *
16  * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
17  *   Use \Drupal\Tests\block_content\Functional\Views\BlockContentTestBase.
18  */
19 abstract class BlockContentTestBase extends ViewTestBase {
20
21   /**
22    * Admin user
23    *
24    * @var object
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', 'block_content_test_views'];
43
44   protected function setUp($import_test_views = TRUE) {
45     parent::setUp($import_test_views);
46     // Ensure the basic bundle exists. This is provided by the standard profile.
47     $this->createBlockContentType(['id' => 'basic']);
48
49     $this->adminUser = $this->drupalCreateUser($this->permissions);
50
51     if ($import_test_views) {
52       ViewTestData::createTestViews(get_class($this), ['block_content_test_views']);
53     }
54   }
55
56   /**
57    * Creates a custom block.
58    *
59    * @param array $settings
60    *   (optional) An associative array of settings for the block_content, as
61    *   used in entity_create().
62    *
63    * @return \Drupal\block_content\Entity\BlockContent
64    *   Created custom block.
65    */
66   protected function createBlockContent(array $settings = []) {
67     $status = 0;
68     $settings += [
69       'info' => $this->randomMachineName(),
70       'type' => 'basic',
71       'langcode' => 'en',
72     ];
73     if ($block_content = BlockContent::create($settings)) {
74       $status = $block_content->save();
75     }
76     $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created block content %info.', ['%info' => $block_content->label()]));
77     return $block_content;
78   }
79
80   /**
81    * Creates a custom block type (bundle).
82    *
83    * @param array $values
84    *   An array of settings to change from the defaults.
85    *
86    * @return \Drupal\block_content\Entity\BlockContentType
87    *   Created custom block type.
88    */
89   protected function createBlockContentType(array $values = []) {
90     // Find a non-existent random type name.
91     if (!isset($values['id'])) {
92       do {
93         $id = strtolower($this->randomMachineName(8));
94       } while (BlockContentType::load($id));
95     }
96     else {
97       $id = $values['id'];
98     }
99     $values += [
100       'id' => $id,
101       'label' => $id,
102       'revision' => FALSE
103     ];
104     $bundle = BlockContentType::create($values);
105     $status = $bundle->save();
106     block_content_add_body_field($bundle->id());
107
108     $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created block content type %bundle.', ['%bundle' => $bundle->id()]));
109     return $bundle;
110   }
111
112 }