|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Beapi\MultisiteSharedBlocks\Tests; |
| 4 | + |
| 5 | +use Beapi\MultisiteSharedBlocks\Parser; |
| 6 | + |
| 7 | +/** |
| 8 | + * @covers \Beapi\MultisiteSharedBlocks\Parser |
| 9 | + */ |
| 10 | +class ParserTest extends \WP_UnitTestCase_Base { |
| 11 | + |
| 12 | + /** |
| 13 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::parse_shared_blocks_from_post_content |
| 14 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::find_shared_blocks |
| 15 | + * |
| 16 | + * @dataProvider data_parse_shared_blocks_from_post_content |
| 17 | + */ |
| 18 | + public function test_parse_shared_blocks_from_post_content( array $shared_blocks, string $post_content, array $excluded_blocks = [] ) { |
| 19 | + $this->assertSame( |
| 20 | + $shared_blocks, |
| 21 | + Parser::parse_shared_blocks_from_post_content( $post_content, $excluded_blocks ) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::get_shared_block |
| 27 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::match_shared_block |
| 28 | + */ |
| 29 | + public function test_find_a_shared_block_in_post_content() { |
| 30 | + $post_data = (object) [ |
| 31 | + 'post_content' => '<!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 32 | + <p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 33 | + <!-- /wp:paragraph --> |
| 34 | + |
| 35 | + <!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true} --> |
| 36 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 37 | + <!-- /wp:paragraph -->', |
| 38 | + ]; |
| 39 | + $_post = new \WP_Post( $post_data ); |
| 40 | + |
| 41 | + $found_block = Parser::get_shared_block( $_post, 'cc38bbdf-f161-459e-8242-b91cd9a5b73f' ); |
| 42 | + $this->assertNotEmpty( $found_block, 'parser found a shared block matching the ID' ); |
| 43 | + $this->assertEquals( 'core/paragraph', $found_block['blockName'], 'shared block is a paragraph' ); |
| 44 | + $this->assertSameSets( |
| 45 | + [ |
| 46 | + 'sharedBlockId' => 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 47 | + 'sharedBlockIsShared' => true, |
| 48 | + ], |
| 49 | + $found_block['attrs'], |
| 50 | + 'shared block has the correct attributes' |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::get_shared_block |
| 56 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::match_shared_block |
| 57 | + * |
| 58 | + * @dataProvider data_non_shared_blocks_should_be_ignore_during_a_match |
| 59 | + */ |
| 60 | + public function test_non_shared_blocks_should_be_ignore_during_a_match( $shared_block_id, $post_content ) { |
| 61 | + $post_data = (object) [ 'post_content' => $post_content ]; |
| 62 | + $_post = new \WP_Post( $post_data ); |
| 63 | + |
| 64 | + $found_block = Parser::get_shared_block( $_post, $shared_block_id ); |
| 65 | + $this->assertEmpty( $found_block ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::get_shared_block |
| 70 | + * @covers \Beapi\MultisiteSharedBlocks\Parser::match_shared_block |
| 71 | + */ |
| 72 | + public function test_excluded_blocks_should_be_ignore_during_a_match() { |
| 73 | + $post_data = (object) [ |
| 74 | + 'post_content' => '<!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 75 | + <p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 76 | + <!-- /wp:paragraph --> |
| 77 | + |
| 78 | + <!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true} --> |
| 79 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 80 | + <!-- /wp:paragraph -->', |
| 81 | + ]; |
| 82 | + $_post = new \WP_Post( $post_data ); |
| 83 | + |
| 84 | + $found_block = Parser::get_shared_block( $_post, 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', [ 'core/paragraph' ] ); |
| 85 | + $this->assertEmpty( $found_block ); |
| 86 | + } |
| 87 | + |
| 88 | + public function data_parse_shared_blocks_from_post_content() { |
| 89 | + return [ |
| 90 | + 'a single shared block with no title' => [ |
| 91 | + [ |
| 92 | + [ |
| 93 | + 'block_id' => 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 94 | + 'block_title' => '', |
| 95 | + ], |
| 96 | + ], |
| 97 | + '<!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 98 | + <p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 99 | + <!-- /wp:paragraph --> |
| 100 | + |
| 101 | + <!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true} --> |
| 102 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 103 | + <!-- /wp:paragraph -->', |
| 104 | + ], |
| 105 | + 'a single shared block with an empty title' => [ |
| 106 | + [ |
| 107 | + [ |
| 108 | + 'block_id' => 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 109 | + 'block_title' => '', |
| 110 | + ], |
| 111 | + ], |
| 112 | + ' |
| 113 | + <!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 114 | +<p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 115 | +<!-- /wp:paragraph --> |
| 116 | +
|
| 117 | +<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true,"sharedBlockShareTitle":""} --> |
| 118 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 119 | +<!-- /wp:paragraph --> |
| 120 | + ', |
| 121 | + ], |
| 122 | + 'a single shared block with a custom title' => [ |
| 123 | + [ |
| 124 | + [ |
| 125 | + 'block_id' => 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 126 | + 'block_title' => "Lorem ispum's text block", |
| 127 | + ], |
| 128 | + ], |
| 129 | + ' |
| 130 | + <!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 131 | +<p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 132 | +<!-- /wp:paragraph --> |
| 133 | +
|
| 134 | +<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true,"sharedBlockShareTitle":"Lorem ispum\'s text block"} --> |
| 135 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 136 | +<!-- /wp:paragraph --> |
| 137 | + ', |
| 138 | + ], |
| 139 | + 'multiple shared blocks' => [ |
| 140 | + [ |
| 141 | + [ |
| 142 | + 'block_id' => 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 143 | + 'block_title' => "Lorem ispum's text block", |
| 144 | + ], |
| 145 | + [ |
| 146 | + 'block_id' => '8d7344de-0e8c-4388-ace2-8921dcaa0c79', |
| 147 | + 'block_title' => '', |
| 148 | + ], |
| 149 | + [ |
| 150 | + 'block_id' => '53989ad0-892e-4a31-9fd2-5eaaa1249100', |
| 151 | + 'block_title' => "Another lorem ispum's text block", |
| 152 | + ], |
| 153 | + ], |
| 154 | + ' |
| 155 | + <!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 156 | +<p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 157 | +<!-- /wp:paragraph --> |
| 158 | +
|
| 159 | +<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true,"sharedBlockShareTitle":"Lorem ispum\'s text block"} --> |
| 160 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 161 | +<!-- /wp:paragraph --> |
| 162 | +
|
| 163 | +<!-- wp:paragraph {"sharedBlockId":"8d7344de-0e8c-4388-ace2-8921dcaa0c79","sharedBlockIsShared":true} --> |
| 164 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 165 | +<!-- /wp:paragraph --> |
| 166 | +
|
| 167 | +<!-- wp:paragraph {"sharedBlockId":"53989ad0-892e-4a31-9fd2-5eaaa1249100","sharedBlockIsShared":true,"sharedBlockShareTitle":"Another lorem ispum\'s text block"} --> |
| 168 | +<p>Egestas sed tempus urna et pharetra. Euismod lacinia at quis risus sed vulputate odio. Egestas quis ipsum suspendisse ultrices gravida.</p> |
| 169 | +<!-- /wp:paragraph --> |
| 170 | + ', |
| 171 | + ], |
| 172 | + 'a single shared block excluded' => [ |
| 173 | + [], |
| 174 | + ' |
| 175 | + <!-- wp:paragraph {"sharedBlockId":"87c748c8-9a97-4b52-9ba6-e6c7670b62f4"} --> |
| 176 | +<p>Adipiscing at in tellus integer feugiat scelerisque varius morbi.</p> |
| 177 | +<!-- /wp:paragraph --> |
| 178 | +
|
| 179 | +<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":true} --> |
| 180 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 181 | +<!-- /wp:paragraph --> |
| 182 | + ', |
| 183 | + [ |
| 184 | + 'core/paragraph', |
| 185 | + ], |
| 186 | + ], |
| 187 | + 'skip inner blocks of a shared block' => [ |
| 188 | + [ |
| 189 | + [ |
| 190 | + 'block_id' => 'fad312a6-7c08-4e9b-8e7c-d197112d9e75', |
| 191 | + 'block_title' => 'Parent block shared', |
| 192 | + ], |
| 193 | + [ |
| 194 | + 'block_id' => 'cca3f7ae-3a30-4c93-a026-e8971f1f314a', |
| 195 | + 'block_title' => 'Another shared block', |
| 196 | + ], |
| 197 | + ], |
| 198 | + ' |
| 199 | + <!-- wp:group {"sharedBlockId":"fad312a6-7c08-4e9b-8e7c-d197112d9e75","sharedBlockIsShared":true,"sharedBlockShareTitle":"Parent block shared","layout":{"type":"constrained"}} --> |
| 200 | +<div class="wp-block-group"><!-- wp:paragraph {"sharedBlockId":"b9162cce-64e3-462f-a682-bf180fad3e24"} --> |
| 201 | +<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut condimentum nisl ac quam blandit pellentesque.</p> |
| 202 | +<!-- /wp:paragraph --> |
| 203 | +
|
| 204 | +<!-- wp:paragraph {"sharedBlockId":"2a8f018f-822d-4d73-897a-bab5b283aa50","sharedBlockIsShared":true,"sharedBlockShareTitle":"Inner block shared"} --> |
| 205 | +<p>Nulla porttitor sem in laoreet elementum. Maecenas venenatis felis enim, at fringilla dui faucibus nec.</p> |
| 206 | +<!-- /wp:paragraph --> |
| 207 | +
|
| 208 | +<!-- wp:paragraph {"sharedBlockId":"96588c6a-eea5-4dc1-acd5-f99935f0c741"} --> |
| 209 | +<p>Ut sagittis neque arcu, a dapibus sem mattis vel. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> |
| 210 | +<!-- /wp:paragraph --></div> |
| 211 | +<!-- /wp:group --> |
| 212 | +
|
| 213 | +<!-- wp:paragraph {"sharedBlockId":"cca3f7ae-3a30-4c93-a026-e8971f1f314a","sharedBlockIsShared":true,"sharedBlockShareTitle":"Another shared block"} --> |
| 214 | +<p>Nulla porttitor sem in laoreet elementum. Maecenas venenatis felis enim, at fringilla dui faucibus nec.</p> |
| 215 | +<!-- /wp:paragraph --> |
| 216 | + ', |
| 217 | + ], |
| 218 | + ]; |
| 219 | + } |
| 220 | + |
| 221 | + public function data_non_shared_blocks_should_be_ignore_during_a_match() { |
| 222 | + return [ |
| 223 | + 'non shared block' => [ |
| 224 | + 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 225 | + '<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f"} --> |
| 226 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 227 | + <!-- /wp:paragraph -->', |
| 228 | + ], |
| 229 | + 'share attribute set to false' => [ |
| 230 | + 'cc38bbdf-f161-459e-8242-b91cd9a5b73f', |
| 231 | + '<!-- wp:paragraph {"sharedBlockId":"cc38bbdf-f161-459e-8242-b91cd9a5b73f","sharedBlockIsShared":false} --> |
| 232 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> |
| 233 | + <!-- /wp:paragraph -->', |
| 234 | + ], |
| 235 | + ]; |
| 236 | + } |
| 237 | +} |
0 commit comments