block-type-registry.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * WP_Block_Type_Registry Tests
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Tests for WP_Block_Type_Registry
  11. *
  12. * @since 5.0.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Test_Block_Type_Registry extends WP_UnitTestCase {
  17. /**
  18. * Fake block type registry.
  19. *
  20. * @since 5.0.0
  21. * @var WP_Block_Type_Registry
  22. */
  23. private $registry = null;
  24. /**
  25. * Set up each test method.
  26. *
  27. * @since 5.0.0
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $this->registry = new WP_Block_Type_Registry();
  32. }
  33. /**
  34. * Tear down each test method.
  35. *
  36. * @since 5.0.0
  37. */
  38. public function tearDown() {
  39. $this->registry = null;
  40. parent::tearDown();
  41. }
  42. /**
  43. * Should reject numbers
  44. *
  45. * @ticket 45097
  46. *
  47. * @expectedIncorrectUsage WP_Block_Type_Registry::register
  48. */
  49. public function test_invalid_non_string_names() {
  50. $result = $this->registry->register( 1, array() );
  51. $this->assertFalse( $result );
  52. }
  53. /**
  54. * Should reject blocks without a namespace
  55. *
  56. * @ticket 45097
  57. *
  58. * @expectedIncorrectUsage WP_Block_Type_Registry::register
  59. */
  60. public function test_invalid_names_without_namespace() {
  61. $result = $this->registry->register( 'paragraph', array() );
  62. $this->assertFalse( $result );
  63. }
  64. /**
  65. * Should reject blocks with invalid characters
  66. *
  67. * @ticket 45097
  68. *
  69. * @expectedIncorrectUsage WP_Block_Type_Registry::register
  70. */
  71. public function test_invalid_characters() {
  72. $result = $this->registry->register( 'still/_doing_it_wrong', array() );
  73. $this->assertFalse( $result );
  74. }
  75. /**
  76. * Should reject blocks with uppercase characters
  77. *
  78. * @ticket 45097
  79. *
  80. * @expectedIncorrectUsage WP_Block_Type_Registry::register
  81. */
  82. public function test_uppercase_characters() {
  83. $result = $this->registry->register( 'Core/Paragraph', array() );
  84. $this->assertFalse( $result );
  85. }
  86. /**
  87. * Should accept valid block names
  88. *
  89. * @ticket 45097
  90. */
  91. public function test_register_block_type() {
  92. $name = 'core/paragraph';
  93. $settings = array(
  94. 'icon' => 'editor-paragraph',
  95. );
  96. $block_type = $this->registry->register( $name, $settings );
  97. $this->assertSame( $name, $block_type->name );
  98. $this->assertSame( $settings['icon'], $block_type->icon );
  99. $this->assertSame( $block_type, $this->registry->get_registered( $name ) );
  100. }
  101. /**
  102. * Should fail to re-register the same block
  103. *
  104. * @ticket 45097
  105. *
  106. * @expectedIncorrectUsage WP_Block_Type_Registry::register
  107. */
  108. public function test_register_block_type_twice() {
  109. $name = 'core/paragraph';
  110. $settings = array(
  111. 'icon' => 'editor-paragraph',
  112. );
  113. $result = $this->registry->register( $name, $settings );
  114. $this->assertNotFalse( $result );
  115. $result = $this->registry->register( $name, $settings );
  116. $this->assertFalse( $result );
  117. }
  118. /**
  119. * Should accept a WP_Block_Type instance
  120. *
  121. * @ticket 45097
  122. */
  123. public function test_register_block_type_instance() {
  124. $block_type = new WP_Fake_Block_Type( 'core/fake' );
  125. $result = $this->registry->register( $block_type );
  126. $this->assertSame( $block_type, $result );
  127. }
  128. /**
  129. * Unregistering should fail if a block is not registered
  130. *
  131. * @ticket 45097
  132. *
  133. * @expectedIncorrectUsage WP_Block_Type_Registry::unregister
  134. */
  135. public function test_unregister_not_registered_block() {
  136. $result = $this->registry->unregister( 'core/unregistered' );
  137. $this->assertFalse( $result );
  138. }
  139. /**
  140. * Should unregister existing blocks
  141. *
  142. * @ticket 45097
  143. */
  144. public function test_unregister_block_type() {
  145. $name = 'core/paragraph';
  146. $settings = array(
  147. 'icon' => 'editor-paragraph',
  148. );
  149. $this->registry->register( $name, $settings );
  150. $block_type = $this->registry->unregister( $name );
  151. $this->assertSame( $name, $block_type->name );
  152. $this->assertSame( $settings['icon'], $block_type->icon );
  153. $this->assertFalse( $this->registry->is_registered( $name ) );
  154. }
  155. /**
  156. * @ticket 45097
  157. */
  158. public function test_get_all_registered() {
  159. $names = array( 'core/paragraph', 'core/image', 'core/blockquote' );
  160. $settings = array(
  161. 'icon' => 'random',
  162. );
  163. foreach ( $names as $name ) {
  164. $this->registry->register( $name, $settings );
  165. }
  166. $registered = $this->registry->get_all_registered();
  167. $this->assertSameSets( $names, array_keys( $registered ) );
  168. }
  169. }