block-list.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * WP_Block_List tests.
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.5.0
  8. */
  9. /**
  10. * Tests for WP_Block_List
  11. *
  12. * @since 5.5.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Block_List_Test extends WP_UnitTestCase {
  17. /**
  18. * Fake block type registry.
  19. *
  20. * @var WP_Block_Type_Registry
  21. */
  22. private $registry = null;
  23. /**
  24. * Set up each test method.
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. $this->registry = new WP_Block_Type_Registry();
  29. $this->registry->register( 'core/example', array() );
  30. }
  31. /**
  32. * Tear down each test method.
  33. */
  34. public function tearDown() {
  35. $this->registry = null;
  36. parent::tearDown();
  37. }
  38. /**
  39. * @ticket 49927
  40. */
  41. function test_array_access() {
  42. $parsed_blocks = parse_blocks( '<!-- wp:example /-->' );
  43. $context = array();
  44. $blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
  45. // Test "offsetExists".
  46. $this->assertTrue( isset( $blocks[0] ) );
  47. // Test "offsetGet".
  48. $this->assertSame( 'core/example', $blocks[0]->name );
  49. // Test "offsetSet".
  50. $parsed_blocks[0]['blockName'] = 'core/updated';
  51. $blocks[0] = new WP_Block( $parsed_blocks[0], $context, $this->registry );
  52. $this->assertSame( 'core/updated', $blocks[0]->name );
  53. // Test "offsetUnset".
  54. unset( $blocks[0] );
  55. $this->assertFalse( isset( $blocks[0] ) );
  56. }
  57. /**
  58. * @ticket 49927
  59. */
  60. function test_iterable() {
  61. $parsed_blocks = parse_blocks( '<!-- wp:example --><!-- wp:example /--><!-- /wp:example -->' );
  62. $context = array();
  63. $blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
  64. $assertions = 0;
  65. foreach ( $blocks as $block ) {
  66. $this->assertSame( 'core/example', $block->name );
  67. $assertions++;
  68. foreach ( $block->inner_blocks as $inner_block ) {
  69. $this->assertSame( 'core/example', $inner_block->name );
  70. $assertions++;
  71. }
  72. }
  73. $blocks->rewind();
  74. while ( $blocks->valid() ) {
  75. $key = $blocks->key();
  76. $block = $blocks->current();
  77. $this->assertSame( 0, $key );
  78. $assertions++;
  79. $this->assertSame( 'core/example', $block->name );
  80. $assertions++;
  81. $blocks->next();
  82. }
  83. $this->assertSame( 4, $assertions );
  84. }
  85. /**
  86. * @ticket 49927
  87. */
  88. function test_countable() {
  89. $parsed_blocks = parse_blocks( '<!-- wp:example /-->' );
  90. $context = array();
  91. $blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
  92. $this->assertSame( 1, count( $blocks ) );
  93. }
  94. }