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