|
12 | 12 | namespace Internet_Archive\Wayback_Machine_Link_Fixer\Tests\Processor; |
13 | 13 |
|
14 | 14 | use Internet_Archive\Wayback_Machine_Link_Fixer\Settings\Settings; |
| 15 | +use Internet_Archive\Wayback_Machine_Link_Fixer\Link\Link_Exclusion; |
15 | 16 | use Internet_Archive\Wayback_Machine_Link_Fixer\Link\Link_Repository; |
16 | 17 | use Internet_Archive\Wayback_Machine_Link_Fixer\WP_Post\WP_Post_Controller; |
17 | 18 |
|
@@ -618,6 +619,133 @@ public function test_excluded_post_does_not_render_link_data(): void { |
618 | 619 | \delete_option( Settings::LINK_FIXER_EXCLUDED_POSTS ); |
619 | 620 | } |
620 | 621 |
|
| 622 | + /** |
| 623 | + * @testdox When a link matches a global exclusion pattern, it should not appear in the render_block data attribute output. |
| 624 | + * |
| 625 | + * @return void |
| 626 | + */ |
| 627 | + public function test_excluded_link_not_included_in_render_block_data(): void { |
| 628 | + // Set the option to render the HTML link output. |
| 629 | + update_option( Settings::FIXER_OPTION, Settings::FIXER_OPTION_REPLACE_LINK ); |
| 630 | + |
| 631 | + // Add a global exclusion pattern that matches one of the links. |
| 632 | + update_option( Settings::LINK_EXCLUSIONS, array( '*excluded-domain.com*' ) ); |
| 633 | + |
| 634 | + // Reset the Link_Exclusion static cache so it picks up the new option. |
| 635 | + $reflection = new \ReflectionClass( Link_Exclusion::class ); |
| 636 | + $property = $reflection->getProperty( 'exclusions' ); |
| 637 | + $property->setAccessible( true ); |
| 638 | + $property->setValue( null, null ); |
| 639 | + |
| 640 | + $post_id = self::factory()->post->create(); |
| 641 | + |
| 642 | + // Add content with two links — one that will be excluded, one that won't. |
| 643 | + $content = 'Link one <a href="https://excluded-domain.com/page">excluded</a> and link two <a href="https://allowed-domain.com/page">allowed</a>'; |
| 644 | + wp_update_post( |
| 645 | + array( |
| 646 | + 'ID' => $post_id, |
| 647 | + 'post_content' => $content, |
| 648 | + 'post_type' => 'post', |
| 649 | + ) |
| 650 | + ); |
| 651 | + |
| 652 | + $GLOBALS['post'] = get_post( $post_id ); |
| 653 | + |
| 654 | + // Render the block. |
| 655 | + $rendered = do_blocks( $GLOBALS['post']->post_content ); |
| 656 | + |
| 657 | + // The data attribute should be present (we still have one non-excluded link). |
| 658 | + $this->assertStringContainsString( 'data-iawmlf-post-links', $rendered ); |
| 659 | + |
| 660 | + // Extract the JSON from the data attribute. |
| 661 | + preg_match( "/data-iawmlf-post-links='([^']*)'/", $rendered, $matches ); |
| 662 | + $this->assertNotEmpty( $matches, 'Should find the data attribute in rendered output.' ); |
| 663 | + |
| 664 | + $links_data = json_decode( html_entity_decode( $matches[1] ), true ); |
| 665 | + $this->assertIsArray( $links_data ); |
| 666 | + |
| 667 | + // Collect all hrefs from the links data. |
| 668 | + $hrefs = array_column( $links_data, 'href' ); |
| 669 | + |
| 670 | + // The excluded link should NOT be in the data. |
| 671 | + $this->assertNotContains( 'https://excluded-domain.com/page', $hrefs, 'Excluded link should not appear in render_block data.' ); |
| 672 | + |
| 673 | + // The allowed link SHOULD be in the data. |
| 674 | + $this->assertContains( 'https://allowed-domain.com/page', $hrefs, 'Non-excluded link should appear in render_block data.' ); |
| 675 | + |
| 676 | + // Clean up. |
| 677 | + unset( $GLOBALS['post'] ); |
| 678 | + \delete_option( Settings::LINK_EXCLUSIONS ); |
| 679 | + |
| 680 | + // Reset the Link_Exclusion static cache. |
| 681 | + $property->setValue( null, null ); |
| 682 | + } |
| 683 | + |
| 684 | + /** |
| 685 | + * @testdox When a link matches a global exclusion pattern and another does not, only the non-excluded link should appear in the render_block data attribute output. |
| 686 | + * |
| 687 | + * @return void |
| 688 | + */ |
| 689 | + public function test_excluded_link_filtered_from_render_block_data_with_mixed_links(): void { |
| 690 | + // Set the option to render the HTML link output. |
| 691 | + update_option( Settings::FIXER_OPTION, Settings::FIXER_OPTION_REPLACE_LINK ); |
| 692 | + |
| 693 | + // Add a global exclusion pattern that matches one of the links. |
| 694 | + update_option( Settings::LINK_EXCLUSIONS, array( '*excluded-domain.com*' ) ); |
| 695 | + |
| 696 | + // Reset the Link_Exclusion static cache so it picks up the new option. |
| 697 | + $reflection = new \ReflectionClass( Link_Exclusion::class ); |
| 698 | + $property = $reflection->getProperty( 'exclusions' ); |
| 699 | + $property->setAccessible( true ); |
| 700 | + $property->setValue( null, null ); |
| 701 | + |
| 702 | + $post_id = self::factory()->post->create(); |
| 703 | + |
| 704 | + // Add content with two links — one excluded, one allowed. |
| 705 | + $content = 'Link one <a href="https://excluded-domain.com/page1">excluded</a> and link two <a href="https://kept-domain.com/page">kept</a>'; |
| 706 | + wp_update_post( |
| 707 | + array( |
| 708 | + 'ID' => $post_id, |
| 709 | + 'post_content' => $content, |
| 710 | + 'post_type' => 'post', |
| 711 | + ) |
| 712 | + ); |
| 713 | + |
| 714 | + $GLOBALS['post'] = get_post( $post_id ); |
| 715 | + |
| 716 | + // Render the block. |
| 717 | + $rendered = do_blocks( $GLOBALS['post']->post_content ); |
| 718 | + |
| 719 | + // The data attribute should be present (we still have one non-excluded link). |
| 720 | + $this->assertStringContainsString( 'data-iawmlf-post-links', $rendered ); |
| 721 | + |
| 722 | + // Extract the JSON from the data attribute. |
| 723 | + preg_match( "/data-iawmlf-post-links='([^']*)'/", $rendered, $matches ); |
| 724 | + $this->assertNotEmpty( $matches, 'Should find the data attribute in rendered output.' ); |
| 725 | + |
| 726 | + $links_data = json_decode( html_entity_decode( $matches[1] ), true ); |
| 727 | + $this->assertIsArray( $links_data ); |
| 728 | + |
| 729 | + // Should only have 1 link (the non-excluded one). |
| 730 | + $this->assertCount( 1, $links_data, 'Only the non-excluded link should be in the data.' ); |
| 731 | + |
| 732 | + // Collect all hrefs from the links data. |
| 733 | + $hrefs = array_column( $links_data, 'href' ); |
| 734 | + |
| 735 | + // The excluded link should NOT be in the data. |
| 736 | + $this->assertNotContains( 'https://excluded-domain.com/page1', $hrefs, 'Excluded link should not appear in render_block data.' ); |
| 737 | + |
| 738 | + // The allowed link SHOULD be in the data. |
| 739 | + $this->assertContains( 'https://kept-domain.com/page', $hrefs, 'Non-excluded link should appear in render_block data.' ); |
| 740 | + |
| 741 | + // Clean up. |
| 742 | + unset( $GLOBALS['post'] ); |
| 743 | + \delete_option( Settings::LINK_EXCLUSIONS ); |
| 744 | + |
| 745 | + // Reset the Link_Exclusion static cache. |
| 746 | + $property->setValue( null, null ); |
| 747 | + } |
| 748 | + |
621 | 749 | /** |
622 | 750 | * @testdox When an option is selected to do nothing, it should not render out the HTML link output. |
623 | 751 | * |
|
0 commit comments