Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

PHP API for registering blocks with Block Lab #434

Merged
merged 17 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions php/blocks/class-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,15 @@ public function add_block( $block_config ) {
* This method should be called during the block_lab_add_blocks action, to ensure
* that the block isn't added too late.
*
* @param array $block_name The name of the block that the field is added to.
* @param string $block_name The name of the block that the field is added to.
* @param array $field_config The config of the field to add.
*/
public function add_field( $block_name, $field_config ) {
if ( ! isset( $this->blocks[ "block-lab/{$block_name}" ] ) ) {
return;
}
if ( ! isset( $field_config['name'] ) ) {
return $blocks;
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it alright that I made this change? Maybe something else was intended.

Copy link
Member Author

@lukecarbis lukecarbis Oct 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, nothing else intended. Good pickup.

}

$this->blocks[ "block-lab/{$block_name}" ]['fields'][ $field_config['name'] ] = $field_config;
Expand Down
67 changes: 67 additions & 0 deletions tests/php/unit/blocks/test-class-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ class Test_Loader extends Abstract_Template {
*/
public $instance;

/**
* A mock block config without a name.
*
* @var array
*/
public $block_config_without_name = array(
'foo' => 'Example Value'
);

/**
* A mock block config with a name.
*
* @var array
*/
public $block_config_with_name = array(
'name' => 'Example Block'
);

/**
* Test init.
*
Expand Down Expand Up @@ -318,6 +336,55 @@ function( $directory ) use ( $overridden_theme_template_path ) {
$this->assertContains( $expected_overriden_template_contents, ob_get_clean() );
}

/**
* Test add_block.
*
* @covers \Block_Lab\Blocks\Loader::add_block()
*/
public function test_add_block() {
// The block config does not have a name, so it should not be added to the $blocks property.
$this->instance->add_block( $this->block_config_without_name );
$this->assertEmpty( $this->get_protected_property( 'blocks' ) );

// Now that the block config has a name, it should be added to the $blocks property.
$this->instance->add_block( $this->block_config_with_name );
$actual_blocks = $this->get_protected_property( 'blocks' );
$this->assertEquals(
$this->block_config_with_name,
$actual_blocks[ "block-lab/{$this->block_config_with_name['name']}" ]
);
}

/**
* Test add_field.
*
* @covers \Block_Lab\Blocks\Loader::add_field()
*/
public function test_add_field() {
$block_name = 'example-block';
$full_block_name = "block-lab/{$block_name}";
$field_name = 'baz-field';
$field_config_with_name = [ 'name' => $field_name ];
$field_config_without_name = [ 'baz' => 'example' ];

// The block does not exist in the $blocks property, so this should not add anything to it.
$this->instance->add_field( $block_name, $field_config_with_name );
$this->assertEmpty( $this->get_protected_property( 'blocks' ) );

// The second argument doesn't have a 'name' value, so this shouldn't add anything to the $blocks property.
$this->instance->add_field( $block_name, $field_config_without_name );
$this->assertEmpty( $this->get_protected_property( 'blocks' ) );

// Now that the block name exists in the $blocks property, this should add the field to it.
$this->set_protected_property( 'blocks', [ $full_block_name => [] ] );
$this->instance->add_field( $block_name, $field_config_with_name );
$actual_blocks = $this->get_protected_property( 'blocks' );
$this->assertEquals(
$field_config_with_name,
$actual_blocks[ $full_block_name ]['fields'][ $field_name ]
);
}

/**
* Gets the full paths of the template CSS files, in order of reverse priority.
*
Expand Down
16 changes: 15 additions & 1 deletion tests/php/unit/helpers/class-abstract-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function invoke_protected_method( $method_name, $args = array() ) {
* Gets a protected property's value.
*
* @param string $property The name of the property to get.
* @return mixed The property value
* @return mixed The property value.
* @throws ReflectionException For a non-accessible property.
*/
public function get_protected_property( $property ) {
Expand All @@ -115,6 +115,20 @@ public function get_protected_property( $property ) {
return $property->getValue( $this->instance );
}

/**
* Sets a protected property's value.
*
* @param string $property The name of the property to get.
* @param mixed $value The new property value.
* @throws ReflectionException For a non-accessible property.
*/
public function set_protected_property( $property, $value ) {
$reflection = new \ReflectionObject( $this->instance );
$property = $reflection->getProperty( $property );
$property->setAccessible( true );
$property->setValue( $this->instance, $value );
}

/**
* Gets the directories that block templates and CSS files could be in.
*/
Expand Down