BlockContentTestBase.php in Drupal 9
File
core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php
View source
<?php
namespace Drupal\Tests\block_content\Functional\Views;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
abstract class BlockContentTestBase extends ViewTestBase {
protected $adminUser;
protected $permissions = [
'administer blocks',
];
protected static $modules = [
'block',
'block_content',
'block_content_test_views',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->createBlockContentType([
'id' => 'basic',
]);
$this->adminUser = $this
->drupalCreateUser($this->permissions);
if ($import_test_views) {
ViewTestData::createTestViews(static::class, [
'block_content_test_views',
]);
}
}
protected function createBlockContent(array $values = []) {
$status = 0;
$values += [
'info' => $this
->randomMachineName(),
'type' => 'basic',
'langcode' => 'en',
];
if ($block_content = BlockContent::create($values)) {
$status = $block_content
->save();
}
$this
->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created block content %info.', [
'%info' => $block_content
->label(),
]));
return $block_content;
}
protected function createBlockContentType(array $values = []) {
if (!isset($values['id'])) {
do {
$id = strtolower($this
->randomMachineName(8));
} while (BlockContentType::load($id));
}
else {
$id = $values['id'];
}
$values += [
'id' => $id,
'label' => $id,
'revision' => FALSE,
];
$bundle = BlockContentType::create($values);
$status = $bundle
->save();
block_content_add_body_field($bundle
->id());
$this
->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created block content type %bundle.', [
'%bundle' => $bundle
->id(),
]));
return $bundle;
}
}