|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Blog Privacy Tests |
| 4 | + * |
| 5 | + * @package automattic/jetpack-mu-wpcom |
| 6 | + */ |
| 7 | + |
| 8 | +declare( strict_types = 1 ); |
| 9 | + |
| 10 | +namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Blog_Privacy; |
| 11 | + |
| 12 | +use Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 13 | + |
| 14 | +require_once Jetpack_Mu_Wpcom::PKG_DIR . 'src/features/blog-privacy/blog-privacy.php'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for the functions defined in the Blog Privacy feature. |
| 18 | + */ |
| 19 | +class Blog_Privacy_Test extends \WorDBless\BaseTestCase { |
| 20 | + /** |
| 21 | + * Post-test suite actions. |
| 22 | + */ |
| 23 | + public static function tear_down_after_class() { |
| 24 | + \delete_option( 'wpcom_data_sharing_opt_out' ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Data provider for ->test_robots_txt(). |
| 29 | + * |
| 30 | + * @return \Iterator |
| 31 | + */ |
| 32 | + public function provide_robots_txt(): \Iterator { |
| 33 | + $ai_blocks = <<<AI_BLOCKS |
| 34 | +User-agent: Amazonbot |
| 35 | +Disallow: / |
| 36 | +
|
| 37 | +User-agent: CCBot |
| 38 | +Disallow: / |
| 39 | +
|
| 40 | +User-agent: FacebookBot |
| 41 | +Disallow: / |
| 42 | +
|
| 43 | +User-agent: Google-Extended |
| 44 | +Disallow: / |
| 45 | +
|
| 46 | +User-agent: GPTBot |
| 47 | +Disallow: / |
| 48 | +
|
| 49 | +User-agent: omgili |
| 50 | +Disallow: / |
| 51 | +
|
| 52 | +User-agent: omgilibot |
| 53 | +Disallow: / |
| 54 | +
|
| 55 | +User-agent: SentiBot |
| 56 | +Disallow: / |
| 57 | +
|
| 58 | +User-agent: sentibot |
| 59 | +Disallow: / |
| 60 | +
|
| 61 | +AI_BLOCKS; |
| 62 | + |
| 63 | + yield 'public, no discourage AI' => array( |
| 64 | + 'blog_public' => '1', |
| 65 | + 'wpcom_data_sharing_opt_out' => null, |
| 66 | + 'expected' => 'TEST', |
| 67 | + ); |
| 68 | + |
| 69 | + yield 'public, discourage AI' => array( |
| 70 | + 'blog_public' => '1', |
| 71 | + 'wpcom_data_sharing_opt_out' => '1', |
| 72 | + 'expected' => "TEST\n$ai_blocks", |
| 73 | + ); |
| 74 | + |
| 75 | + yield 'discourage search, no discourage AI' => array( |
| 76 | + 'blog_public' => '0', |
| 77 | + 'wpcom_data_sharing_opt_out' => null, |
| 78 | + 'expected' => 'TEST', |
| 79 | + ); |
| 80 | + |
| 81 | + yield 'discourage search, discourage AI' => array( |
| 82 | + 'blog_public' => '0', |
| 83 | + 'wpcom_data_sharing_opt_out' => '1', |
| 84 | + 'expected' => 'TEST', // We already discourage all user agents. |
| 85 | + ); |
| 86 | + |
| 87 | + yield 'private, no discourage AI' => array( |
| 88 | + 'blog_public' => '-1', |
| 89 | + 'wpcom_data_sharing_opt_out' => null, |
| 90 | + 'expected' => 'TEST', |
| 91 | + ); |
| 92 | + |
| 93 | + yield 'private, discourage AI' => array( |
| 94 | + 'blog_public' => '-1', |
| 95 | + 'wpcom_data_sharing_opt_out' => '1', |
| 96 | + 'expected' => 'TEST', // Private overrides wpcom_data_sharing_opt_out setting. |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test for robots_txt hook. |
| 102 | + */ |
| 103 | + public function test_robots_txt_is_hooked() { |
| 104 | + $this->assertSame( 12, \has_filter( 'robots_txt', __NAMESPACE__ . '\\robots_txt' ) ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Tests for robots_txt(). |
| 109 | + * |
| 110 | + * @dataProvider provide_robots_txt |
| 111 | + * @param mixed $blog_public The value of the blog_public option. |
| 112 | + * @param mixed|null $wpcom_data_sharing_opt_out The value of the wpcom_data_sharing_opt_out option (null if the option is not set). |
| 113 | + * @param string $expected The expected output for robots.txt. |
| 114 | + */ |
| 115 | + public function test_robots_txt( $blog_public, $wpcom_data_sharing_opt_out, string $expected ) { |
| 116 | + if ( null === $wpcom_data_sharing_opt_out ) { |
| 117 | + \delete_option( 'wpcom_data_sharing_opt_out' ); |
| 118 | + } else { |
| 119 | + \update_option( 'wpcom_data_sharing_opt_out', $wpcom_data_sharing_opt_out ); |
| 120 | + } |
| 121 | + |
| 122 | + $this->assertSame( $expected, robots_txt( 'TEST', $blog_public ) ); |
| 123 | + } |
| 124 | +} |
0 commit comments