Add BlockInterface::remove()

This commit is contained in:
Matt Sherman 2023-09-13 08:13:42 -04:00
parent 0b83163d9c
commit 514f96cbfe
3 changed files with 33 additions and 0 deletions

View File

@ -70,6 +70,11 @@ interface BlockInterface {
*/
public function &get_root_template(): BlockTemplateInterface;
/**
* Remove the block from its parent.
*/
public function remove();
/**
* Check if the block is detached from its parent or root template.
*

View File

@ -173,6 +173,13 @@ class AbstractBlock implements BlockInterface {
return $this->parent;
}
/**
* Remove the block from its parent.
*/
public function remove() {
$this->parent->remove_block( $this->id );
}
/**
* Check if the block is detached from its parent block container or the template it belongs to.
*

View File

@ -232,6 +232,27 @@ class BlockTest extends WC_Unit_Test_Case {
);
}
/**
* Test that removing a block by calling remove on it detaches it.
*/
public function test_remove_block_self() {
$template = new BlockTemplate();
$block = $template->add_block(
[
'id' => 'test-block-id',
'blockName' => 'test-block-name',
]
);
$block->remove();
$this->assertTrue(
$block->is_detached(),
'Failed asserting that the block is detached from its parent and root template.'
);
}
/**
* Test that adding nested blocks sets the parent and root template correctly.
*/