Hello, there
', + 'I am a new text', + 'I am a new text
', + 2, + ], + ]; + } + + /** + * @dataProvider provider_test_set_modifiable_text_invalid_nodes + */ + public function test_set_modifiable_text_refuses_to_process_unsupported_nodes( $markup ) { + $p = new WP_Block_Markup_Processor( $markup ); + $p->next_token(); + $this->assertFalse( $p->set_modifiable_text( 'New text' ), 'Set the modifiable text on an unsupported node.' ); + } + + + static public function provider_test_set_modifiable_text_invalid_nodes() { + return [ + 'Tag' => [''], + 'DOCTYPE' => [''], + 'Funky comment' => ['1I am a comment>'], + ]; + } + + public function test_set_modifiable_text_can_be_called_twice() { + $p = new WP_Block_Markup_Processor( 'Hey there
' ); + $p->next_token(); + $p->next_token(); + $this->assertTrue( $p->set_modifiable_text( 'This is the new text, it is much longer' ), 'Failed to set the modifiable text.' ); + $this->assertEquals( + 'This is the new text, it is much longer
', + $p->get_updated_html(), + 'Failed to set the modifiable text.' + ); + + $this->assertTrue( $p->set_modifiable_text( 'Back to short text :)' ), 'Failed to set the modifiable text.' ); + $this->assertEquals( + 'Back to short text :)
', + $p->get_updated_html(), + 'Failed to set the modifiable text.' + ); + } + + public function test_next_block_attribute_finds_the_first_attribute() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + + $this->assertEquals( 'class', $p->get_block_attribute_key(), 'Failed to find the block attribute name' ); + $this->assertEquals( 'wp-bold', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + } + + public function test_next_block_attribute_finds_the_second_attribute() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $this->assertEquals( 'id', $p->get_block_attribute_key(), 'Failed to find the block attribute name' ); + $this->assertEquals( 'New York City', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + } + + public function test_next_block_attribute_finds_nested_attributes() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $this->assertEquals( 'lowres', $p->get_block_attribute_key(), 'Failed to find the block attribute name' ); + $this->assertEquals( 'small.png', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $this->assertEquals( 'hires', $p->get_block_attribute_key(), 'Failed to find the block attribute name' ); + $this->assertEquals( 'large.png', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + } + + public function test_next_block_attribute_finds_top_level_attributes_after_nesting() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $this->assertEquals( 'class', $p->get_block_attribute_key(), 'Failed to find the block attribute name' ); + $this->assertEquals( 'wp-bold', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + } + + public function test_set_block_attribute_value_updates_a_simple_attribute() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + + $p->set_block_attribute_value( 'wp-italics' ); + $this->assertEquals( '', $p->get_updated_html(), + 'Failed to update the block attribute value' ); + } + + public function test_set_block_attribute_value_updates_affects_get_block_attribute_value() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + + $p->set_block_attribute_value( 'wp-italics' ); + $this->assertEquals( 'wp-italics', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + } + + public function test_set_block_attribute_value_updates_a_nested_attribute() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $p->set_block_attribute_value( 'medium.png' ); + $this->assertEquals( 'medium.png', $p->get_block_attribute_value(), 'Failed to find the block attribute value' ); + $this->assertEquals( '', $p->get_updated_html(), + 'Failed to update the block attribute value' ); + } + + public function test_set_block_attribute_can_be_called_multiple_times() { + $p = new WP_Block_Markup_Processor( + '' + ); + $this->assertTrue( $p->next_token(), 'Failed to find the block opener' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' ); + $this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' ); + + $p->set_block_attribute_value( 'medium.png' ); + $p->set_block_attribute_value( 'oh-completely-different-image.png' ); + $this->assertEquals( 'oh-completely-different-image.png', $p->get_block_attribute_value(), + 'Failed to find the block attribute value' ); + $this->assertEquals( + '', + $p->get_updated_html(), + 'Failed to update the block attribute value' + ); + } } diff --git a/transfer-protocol/tests/WP_Block_Markup_Url_Processor_Tests.php b/transfer-protocol/tests/WP_Block_Markup_Url_Processor_Tests.php index 74f527b..e57a0f7 100644 --- a/transfer-protocol/tests/WP_Block_Markup_Url_Processor_Tests.php +++ b/transfer-protocol/tests/WP_Block_Markup_Url_Processor_Tests.php @@ -74,6 +74,18 @@ static public function provider_test_finds_next_url() 'https://wordpress.org', 'Have you seen https://wordpress.org? ' ], + 'In a text node, when it contains a protocol-relative absolute URL' => [ + '//wordpress.org', + 'Have you seen //wordpress.org? ' + ], + 'In a text node, when it contains a domain-only absolute URL' => [ + 'wordpress.org', + 'Have you seen wordpress.org? ' + ], + 'In a text node, when it contains a domain-only absolute URL with path' => [ + 'wordpress.org/plugins', + 'Have you seen wordpress.org/plugins? ' + ], ]; } @@ -108,4 +120,84 @@ public function test_next_url_finds_urls_in_multiple_tags( ) { $this->assertEquals( 'https://third-url.org', $p->get_url(), 'Found a URL in the markup, but it wasn\'t the expected one.' ); } + /** + * + * @dataProvider provider_test_set_url_examples + */ + public function test_set_url($markup, $new_url, $new_markup) + { + $p = new WP_Block_Markup_Url_Processor($markup); + $this->assertTrue($p->next_url(), 'Failed to find the URL in the markup.'); + $this->assertTrue($p->set_url($new_url), 'Failed to set the URL in the markup.'); + $this->assertEquals($new_markup, $p->get_updated_html(), 'Failed to set the URL in the markup.'); + } + + static public function provider_test_set_url_examples() + { + return [ + 'In the href attribute of an tag' => [ + '', + 'https://w.org', + '' + ], + 'In the "src" block attribute' => [ + '', + 'https://w.org', + '' + ], + 'In a text node' => [ + 'Have you seen https://wordpress.org yet?', + 'https://w.org', + 'Have you seen https://w.org yet?' + ], + ]; + } + + public function test_set_url_complex_test_case() + { + $p = new WP_Block_Markup_Url_Processor(<< + + + + +During the Write of Passage, I stubbornly tried to beat my writer’s block by writing until 3am multiple times. The burnout returned. I dropped everything and went to Greece for a week.
+ + + ++Have you seen my blog, adamadam.blog? I told a story there of how I got my Bachelor's degree, +check it out: https://adamadam.blog/2021/09/16/how-i-got-bachelors-in-six-months/ +
+ +HTML + ); + + // Replace every url with 'https://site-export.internal' + while($p->next_url()) { + $p->set_url('https://site-export.internal'); + } + + $this->assertEquals( + << + + + + +During the Write of Passage, I stubbornly tried to beat my writer’s block by writing until 3am multiple times. The burnout returned. I dropped everything and went to Greece for a week.
+ + + ++Have you seen my blog, https://site-export.internal? I told a story there of how I got my Bachelor's degree, +check it out: https://site-export.internal +
+ +HTML, + $p->get_updated_html(), + 'Failed to update all the URLs in the markup.' + ); + } + } diff --git a/transfer-protocol/tests/WP_Migration_URL_In_Text_Processor_Tests.php b/transfer-protocol/tests/WP_Migration_URL_In_Text_Processor_Tests.php new file mode 100644 index 0000000..601156c --- /dev/null +++ b/transfer-protocol/tests/WP_Migration_URL_In_Text_Processor_Tests.php @@ -0,0 +1,124 @@ +assertTrue($p->next_url(), 'Failed to find the URL in the text.'); + } + $this->assertEquals($url, $p->get_url(), 'Found a URL in the text, but it wasn\'t the expected one.'); + } + + static public function provider_test_finds_next_url() + { + return [ + 'Absolute URL' => ['https://wordpress.org', 'Have you seen https://wordpress.org?'], + 'Second absolute URL' => ['https://w.org', 'Have you seen https://wordpress.org or https://w.org?', 1], + 'Domain-only' => ['www.example.com', 'Visit www.example.com'], + 'Domain + path' => ['www.example.com/path', 'Visit www.example.com/path'], + 'UTF-8 domain' => ['łąka.pl', 'Więcej na łąka.pl'], + 'ASCII path' => ['https://w.org/plugins', 'Visit the WordPress plugins directory https://w.org/plugins'], + 'Urlencoded query' => ['https://w.org/plugins?%C5%82%C4%85ka=1', 'Visit the WordPress plugins directory https://w.org/plugins?%C5%82%C4%85ka=1'], + 'UTF-8 characters in the query' => ['https://w.org/plugins?łąka=1', 'Visit the WordPress plugins directory https://w.org/plugins?łąka=1'], + 'UTF-8 characters in the path' => ['https://w.org/łąka', 'Visit the WordPress plugins directory https://w.org/łąka'], + 'Closing parenthesis after the path' => ['https://w.org/plugins', 'Visit the WordPress plugins directory (https://w.org/plugins)'], + 'Parenthesis within the path' => ['https://w.org/plug(in)s', 'Visit the WordPress plugins directory (https://w.org/plug(in)s'], + 'Protocol-relative URL' => ['//w.org/', 'Visit the WordPress org at //w.org/ '], + ]; + } + + public function test_set_url_returns_true_on_success() { + $p = new WP_Migration_URL_In_Text_Processor('Have you seen https://wordpress.org?'); + $p->next_url(); + $this->assertTrue($p->set_url('https://w.org'), 'Failed to set the URL in the text.'); + } + + public function test_set_url_returns_false_on_failure() { + $p = new WP_Migration_URL_In_Text_Processor('Have you seen WordPress?'); + $p->next_url(); + $this->assertFalse($p->set_url('https://w.org'), 'set_url returned true when no URL was matched.'); + } + + /** + * + * @dataProvider provider_test_set_url_data + */ + public function test_set_url_replaces_the_url( $text, $new_url, $expected_text ) { + $p = new WP_Migration_URL_In_Text_Processor($text); + $p->next_url(); + $p->set_url($new_url); + $this->assertEquals( + $new_url, + $p->get_url(), + 'Failed to set the URL in the text.' + ); + $this->assertEquals( + $expected_text, + $p->get_updated_text(), + 'Failed to set the URL in the text.' + ); + } + + static public function provider_test_set_url_data() + { + return [ + 'Replace with HTTPS URL' => [ + 'Have you seen https://wordpress.org (or wp.org)?', + 'https://wikipedia.org', + 'Have you seen https://wikipedia.org (or wp.org)?', + ], + 'Replace with a protocol-relative URL' => [ + 'Have you seen https://wordpress.org (or wp.org)?', + '//wikipedia.org', + 'Have you seen //wikipedia.org (or wp.org)?', + ], + 'Replace with a schema-less URL' => [ + 'Have you seen https://wordpress.org (or wp.org)?', + 'wikipedia.org', + 'Have you seen wikipedia.org (or wp.org)?', + ], + ]; + } + + public function test_set_url_can_be_called_twice( ) { + $p = new WP_Migration_URL_In_Text_Processor('Have you seen https://wordpress.org (or w.org)?'); + $p->next_url(); + $p->set_url('https://developer.wordpress.org'); + $p->get_updated_text(); + $p->set_url('https://wikipedia.org'); + $this->assertEquals( + 'https://wikipedia.org', + $p->get_url(), + 'Failed to set the URL in the text.' + ); + $this->assertEquals( + 'Have you seen https://wikipedia.org (or w.org)?', + $p->get_updated_text(), + 'Failed to set the URL in the text.' + ); + } + + public function test_set_url_can_be_called_twice_before_moving_on( ) { + $p = new WP_Migration_URL_In_Text_Processor('Have you seen https://wordpress.org (or w.org)?'); + $p->next_url(); + $p->set_url('https://wikipedia.org'); + $p->get_updated_text(); + $p->set_url('https://developer.wordpress.org'); + $p->next_url(); + $p->set_url('https://meetups.wordpress.org'); + $this->assertEquals( + 'Have you seen https://developer.wordpress.org (or https://meetups.wordpress.org)?', + $p->get_updated_text(), + 'Failed to set the URL in the text.' + ); + } +} diff --git a/transfer-protocol/tests/WP_URL_In_Text_Processor_Tests.php b/transfer-protocol/tests/WP_URL_In_Text_Processor_Tests.php deleted file mode 100644 index 17acf20..0000000 --- a/transfer-protocol/tests/WP_URL_In_Text_Processor_Tests.php +++ /dev/null @@ -1,35 +0,0 @@ -assertTrue($p->next_url(), 'Failed to find the URL in the text.'); - } - $this->assertEquals($url, $p->get_url(), 'Found a URL in the text, but it wasn\'t the expected one.'); - } - - static public function provider_test_finds_next_url() - { - return [ - 'Absolute URL' => ['https://wordpress.org', 'Have you seen https://wordpress.org?'], - 'Second absolute URL' => ['https://w.org', 'Have you seen https://wordpress.org or https://w.org?', 1], - 'Domain-only' => ['www.example.com', 'Visit www.example.com'], - 'Domain + path' => ['www.example.com/path', 'Visit www.example.com/path'], - 'UTF-8 domain' => ['łąka.pl', 'Więcej na łąka.pl'], - 'ASCII path' => ['https://w.org/plugins?', 'Visit the WordPress plugins directory https://w.org/plugins?łąka=1'], - 'Encoded path' => ['https://w.org/plugins?%C5%82%C4%85ka=1', 'Visit the WordPress plugins directory https://w.org/plugins?%C5%82%C4%85ka=1'], - 'Closing parenthesis after the path' => ['https://w.org/plugins', 'Visit the WordPress plugins directory (https://w.org/plugins)'], - 'Parenthesis within the path' => ['https://w.org/plug(in)s', 'Visit the WordPress plugins directory (https://w.org/plug(in)s'], - ]; - } -}