BlockContentSaveTest.php in Drupal 9
File
core/modules/block_content/tests/src/Functional/BlockContentSaveTest.php
View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContent;
class BlockContentSaveTest extends BlockContentTestBase {
protected static $modules = [
'block_content_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->adminUser);
}
public function testImport() {
$max_id = (int) \Drupal::entityQueryAggregate('block_content')
->accessCheck(FALSE)
->aggregate('id', 'max')
->execute()[0]['id_max'];
$test_id = $max_id + mt_rand(1000, 1000000);
$info = $this
->randomMachineName(8);
$block_array = [
'info' => $info,
'body' => [
'value' => $this
->randomMachineName(32),
],
'type' => 'basic',
'id' => $test_id,
];
$block = BlockContent::create($block_array);
$block
->enforceIsNew(TRUE);
$block
->save();
$this
->assertEquals($test_id, $block
->id(), 'Block imported using provide id');
$block_by_id = BlockContent::load($test_id);
$this
->assertNotEmpty($block_by_id, 'Custom block load by block ID.');
$this
->assertSame($block_array['body']['value'], $block_by_id->body->value);
}
public function testDeterminingChanges() {
$block = $this
->createBlockContent('test_changes');
$this
->assertEquals(REQUEST_TIME, $block
->getChangedTime(), 'Creating a block sets default "changed" timestamp.');
$block
->save();
$this
->assertEquals('test_changes', $block
->label(), 'No changes have been determined.');
$block
->setInfo('updated');
$block
->save();
$this
->assertEquals('updated_presave_update', $block
->label(), 'Changes have been determined.');
$this
->assertEquals(979534800, $block
->getChangedTime(), 'Saving a custom block uses "changed" timestamp set in presave hook.');
$block = BlockContent::load($block
->id());
$this
->assertEquals('updated_presave', $block
->label(), 'Static cache has been cleared.');
}
public function testBlockContentSaveOnInsert() {
$block = $this
->createBlockContent('new');
$this
->assertEquals('BlockContent ' . $block
->id(), $block
->label(), 'Custom block saved on block insert.');
}
}