3 namespace Drupal\block_content\Tests\Views;
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;
12 * Base class for all block_content tests.
14 abstract class BlockContentTestBase extends ViewTestBase {
24 * Permissions to grant admin user.
28 protected $permissions = [
37 public static $modules = ['block', 'block_content', 'block_content_test_views'];
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']);
44 $this->adminUser = $this->drupalCreateUser($this->permissions);
46 if ($import_test_views) {
47 ViewTestData::createTestViews(get_class($this), ['block_content_test_views']);
52 * Creates a custom block.
54 * @param array $settings
55 * (optional) An associative array of settings for the block_content, as
56 * used in entity_create().
58 * @return \Drupal\block_content\Entity\BlockContent
59 * Created custom block.
61 protected function createBlockContent(array $settings = []) {
64 'info' => $this->randomMachineName(),
68 if ($block_content = BlockContent::create($settings)) {
69 $status = $block_content->save();
71 $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created block content %info.', ['%info' => $block_content->label()]));
72 return $block_content;
76 * Creates a custom block type (bundle).
78 * @param array $values
79 * An array of settings to change from the defaults.
81 * @return \Drupal\block_content\Entity\BlockContentType
82 * Created custom block type.
84 protected function createBlockContentType(array $values = []) {
85 // Find a non-existent random type name.
86 if (!isset($values['id'])) {
88 $id = strtolower($this->randomMachineName(8));
89 } while (BlockContentType::load($id));
99 $bundle = BlockContentType::create($values);
100 $status = $bundle->save();
101 block_content_add_body_field($bundle->id());
103 $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created block content type %bundle.', ['%bundle' => $bundle->id()]));