mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
Blocks: Add the reusable block post type, wp_block.
Merges [43804] from the 5.0 branch to trunk. See #45098. git-svn-id: https://develop.svn.wordpress.org/trunk@44146 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
102
tests/phpunit/tests/blocks/render-reusable.php
Normal file
102
tests/phpunit/tests/blocks/render-reusable.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Reusable block rendering tests.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 5.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for reusable block rendering.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @group blocks
|
||||
*/
|
||||
class WP_Test_Render_Reusable_Blocks extends WP_UnitTestCase {
|
||||
/**
|
||||
* Fake user ID.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $user_id;
|
||||
|
||||
/**
|
||||
* Fake block ID.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $block_id;
|
||||
|
||||
/**
|
||||
* Fake post ID.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $post_id;
|
||||
|
||||
/**
|
||||
* Create fake data before tests run.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$user_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'editor',
|
||||
)
|
||||
);
|
||||
|
||||
self::$post_id = $factory->post->create(
|
||||
array(
|
||||
'post_author' => self::$user_id,
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Test Post',
|
||||
'post_content' => '<p>Hello world!</p>',
|
||||
)
|
||||
);
|
||||
|
||||
self::$block_id = $factory->post->create(
|
||||
array(
|
||||
'post_author' => self::$user_id,
|
||||
'post_type' => 'wp_block',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Test Block',
|
||||
'post_content' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete fake data after tests run.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$block_id, true );
|
||||
wp_delete_post( self::$post_id, true );
|
||||
self::delete_user( self::$user_id );
|
||||
}
|
||||
|
||||
public function test_render() {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
|
||||
$output = $block_type->render( array( 'ref' => self::$block_id ) );
|
||||
$this->assertSame( '<p>Hello world!</p>', $output );
|
||||
}
|
||||
|
||||
public function test_ref_empty() {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
|
||||
$output = $block_type->render( array() );
|
||||
$this->assertSame( '', $output );
|
||||
}
|
||||
|
||||
public function test_ref_wrong_post_type() {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
|
||||
$output = $block_type->render( array( 'ref' => self::$post_id ) );
|
||||
$this->assertSame( '', $output );
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
);
|
||||
protected static $super_admin = null;
|
||||
|
||||
protected static $block_id;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$users = array(
|
||||
'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
|
||||
@@ -27,6 +29,16 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
);
|
||||
self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) );
|
||||
grant_super_admin( self::$super_admin->ID );
|
||||
|
||||
self::$block_id = $factory->post->create(
|
||||
array(
|
||||
'post_author' => self::$users['administrator']->ID,
|
||||
'post_type' => 'wp_block',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Test Block',
|
||||
'post_content' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
@@ -36,6 +48,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$block_id, true );
|
||||
}
|
||||
|
||||
|
||||
function _flush_roles() {
|
||||
// we want to make sure we're testing against the db, not just in-memory data
|
||||
// this will flush everything and reload it from the db
|
||||
@@ -2095,4 +2112,76 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||
|
||||
$this->assertSame( 333, $roles->get_site_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_block_caps
|
||||
*/
|
||||
function test_block_caps( $role, $cap, $use_post, $expected ) {
|
||||
if ( $use_post ) {
|
||||
$this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap, self::$block_id ) );
|
||||
} else {
|
||||
$this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap ) );
|
||||
}
|
||||
}
|
||||
|
||||
function data_block_caps() {
|
||||
$post_caps = array(
|
||||
'edit_block',
|
||||
'read_block',
|
||||
'delete_block',
|
||||
);
|
||||
|
||||
$all_caps = array(
|
||||
'edit_block',
|
||||
'read_block',
|
||||
'delete_block',
|
||||
'edit_blocks',
|
||||
'edit_others_blocks',
|
||||
'publish_blocks',
|
||||
'read_private_blocks',
|
||||
'delete_blocks',
|
||||
'delete_private_blocks',
|
||||
'delete_published_blocks',
|
||||
'delete_others_blocks',
|
||||
'edit_private_blocks',
|
||||
'edit_published_blocks',
|
||||
);
|
||||
|
||||
$roles = array(
|
||||
'administrator' => $all_caps,
|
||||
'editor' => $all_caps,
|
||||
'author' => array(
|
||||
'read_block',
|
||||
'edit_blocks',
|
||||
'publish_blocks',
|
||||
'delete_blocks',
|
||||
'delete_published_blocks',
|
||||
'edit_published_blocks',
|
||||
),
|
||||
'contributor' => array(
|
||||
'read_block',
|
||||
'edit_blocks',
|
||||
'delete_blocks',
|
||||
),
|
||||
'subscriber' => array(),
|
||||
);
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ( $roles as $role => $caps ) {
|
||||
foreach ( $caps as $cap ) {
|
||||
$use_post = in_array( $cap, $post_caps, true );
|
||||
$data[] = array( $role, $cap, $use_post, true );
|
||||
}
|
||||
|
||||
foreach ( $all_caps as $cap ) {
|
||||
if ( ! in_array( $cap, $caps, true ) ) {
|
||||
$use_post = in_array( $cap, $post_caps, true );
|
||||
$data[] = array( $role, $cap, $use_post, false );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user