|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test file for Sanitize class. |
| 4 | + * |
| 5 | + * @package Activitypub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests; |
| 9 | + |
| 10 | +use Activitypub\Sanitize; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test class for Sanitize. |
| 14 | + * |
| 15 | + * @coversDefaultClass \Activitypub\Sanitize |
| 16 | + */ |
| 17 | +class Test_Sanitize extends \WP_UnitTestCase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Data provider for URL list tests. |
| 21 | + * |
| 22 | + * @return array Test data. |
| 23 | + */ |
| 24 | + public function url_list_provider() { |
| 25 | + return array( |
| 26 | + 'duplicate_urls' => array( |
| 27 | + array( |
| 28 | + 'https://example.com', |
| 29 | + 'https://example.com', |
| 30 | + 'not-a-url', |
| 31 | + 'https://wordpress.org', |
| 32 | + ), |
| 33 | + array( |
| 34 | + 'https://example.com', |
| 35 | + 'http://not-a-url', |
| 36 | + 'https://wordpress.org', |
| 37 | + ), |
| 38 | + ), |
| 39 | + 'mixed_urls_in_string_whitespace' => array( |
| 40 | + "https://example.com\nnot-a-url\nhttps://wordpress.org ", |
| 41 | + array( |
| 42 | + 'https://example.com', |
| 43 | + 'http://not-a-url', |
| 44 | + 'https://wordpress.org', |
| 45 | + ), |
| 46 | + ), |
| 47 | + 'special_characters' => array( |
| 48 | + array( |
| 49 | + 'https://example.com/path with spaces ', |
| 50 | + 'https://example.com/über/path', |
| 51 | + 'https://example.com/path?param=value¶m2=value2#section', |
| 52 | + ), |
| 53 | + array( |
| 54 | + 'https://example.com/path%20with%20spaces', |
| 55 | + 'https://example.com/über/path', |
| 56 | + 'https://example.com/path?param=value¶m2=value2#section', |
| 57 | + ), |
| 58 | + ), |
| 59 | + 'empty_array' => array( array(), array() ), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test url_list with various inputs. |
| 65 | + * |
| 66 | + * @dataProvider url_list_provider |
| 67 | + * @covers ::url_list |
| 68 | + * |
| 69 | + * @param mixed $input Input value. |
| 70 | + * @param array $expected Expected output. |
| 71 | + */ |
| 72 | + public function test_url_list( $input, $expected ) { |
| 73 | + $this->assertEquals( $expected, Sanitize::url_list( $input ) ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Data provider for host list tests. |
| 78 | + * |
| 79 | + * @return array Test data. |
| 80 | + */ |
| 81 | + public function host_list_provider() { |
| 82 | + return array( |
| 83 | + 'single_valid_host' => array( |
| 84 | + 'example.com', |
| 85 | + 'example.com', |
| 86 | + ), |
| 87 | + 'multiple_valid_hosts' => array( |
| 88 | + "ftp://example.com\nhttp://wordpress.org\nhttps://test.example.com", |
| 89 | + "example.com\nwordpress.org\ntest.example.com", |
| 90 | + ), |
| 91 | + 'mixed_case_hosts' => array( |
| 92 | + "ExAmPlE.cOm\nWoRdPrEsS.oRg", |
| 93 | + "example.com\nwordpress.org", |
| 94 | + ), |
| 95 | + 'invalid_hosts' => array( |
| 96 | + " not-a-domain\n\nexample.com\n\t@invalid.com", |
| 97 | + "not-a-domain\nexample.com\ninvalid.com", |
| 98 | + ), |
| 99 | + 'empty_string' => array( |
| 100 | + '', |
| 101 | + '', |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test host_list with various inputs. |
| 108 | + * |
| 109 | + * @dataProvider host_list_provider |
| 110 | + * @covers ::host_list |
| 111 | + * |
| 112 | + * @param string $input Input value. |
| 113 | + * @param string $expected Expected output. |
| 114 | + */ |
| 115 | + public function test_host_list( $input, $expected ) { |
| 116 | + $this->assertEquals( $expected, Sanitize::host_list( $input ) ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Data provider for blog identifier tests. |
| 121 | + * |
| 122 | + * @return array Test data. |
| 123 | + */ |
| 124 | + public function blog_identifier_provider() { |
| 125 | + return array( |
| 126 | + 'simple_string' => array( 'test-Blog', 'test-blog' ), |
| 127 | + 'with_spaces' => array( 'test blog', 'test-blog' ), |
| 128 | + 'with_dots' => array( 'test.blog', 'test.blog' ), |
| 129 | + 'special_chars' => array( 'test@#$%^&*blog', 'testblog' ), |
| 130 | + 'multiple_dots' => array( 'test.blog.name', 'test.blog.name' ), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Test blog_identifier with various inputs. |
| 136 | + * |
| 137 | + * @dataProvider blog_identifier_provider |
| 138 | + * @covers ::blog_identifier |
| 139 | + * |
| 140 | + * @param string $input Input value. |
| 141 | + * @param string $expected Expected output. |
| 142 | + */ |
| 143 | + public function test_blog_identifier( $input, $expected ) { |
| 144 | + $this->assertEquals( $expected, Sanitize::blog_identifier( $input ) ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test blog_identifier with an existing username. |
| 149 | + * |
| 150 | + * @covers ::blog_identifier |
| 151 | + */ |
| 152 | + public function test_blog_identifier_with_existing_user() { |
| 153 | + $user_id = self::factory()->user->create( |
| 154 | + array( |
| 155 | + 'user_login' => 'existing-user', |
| 156 | + 'user_nicename' => 'test-nicename', |
| 157 | + ) |
| 158 | + ); |
| 159 | + |
| 160 | + $result = Sanitize::blog_identifier( 'existing-user' ); |
| 161 | + |
| 162 | + $this->assertEquals( \Activitypub\Model\Blog::get_default_username(), $result ); |
| 163 | + $this->assertNotEmpty( get_settings_errors( 'activitypub_blog_identifier' ) ); |
| 164 | + |
| 165 | + // Reset. |
| 166 | + $GLOBALS['wp_settings_errors'] = array(); |
| 167 | + |
| 168 | + $result = Sanitize::blog_identifier( 'test-nicename' ); |
| 169 | + |
| 170 | + $this->assertEquals( \Activitypub\Model\Blog::get_default_username(), $result ); |
| 171 | + $this->assertNotEmpty( get_settings_errors( 'activitypub_blog_identifier' ) ); |
| 172 | + |
| 173 | + \wp_delete_user( $user_id ); |
| 174 | + } |
| 175 | +} |
0 commit comments