Skip to content

Commit 79f49e5

Browse files
committed
Tests: Cover sitemap provider filtering and registry lookups.
Test replacement and rejection of providers through `wp_sitemaps_add_provider`. Verify unregistered provider lookups and empty and multiple-provider registry results using strict assertions. Developed in: #13976 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64085 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f3cf7a0 commit 79f49e5

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

‎tests/phpunit/tests/sitemaps/wpSitemapsRegistry.php‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,112 @@ public function data_get_provider_should_return_null_with_non_string_name() {
6767
'float (1.1)' => array( 1.1 ),
6868
);
6969
}
70+
71+
/**
72+
* Tests that the sitemap provider can be filtered before being added.
73+
*
74+
* @covers WP_Sitemaps_Registry::add_provider
75+
*/
76+
public function test_add_provider_filter() {
77+
$original_provider = new WP_Sitemaps_Test_Provider();
78+
$filtered_provider = new WP_Sitemaps_Test_Provider();
79+
80+
add_filter(
81+
'wp_sitemaps_add_provider',
82+
static function () use ( $filtered_provider ) {
83+
return $filtered_provider;
84+
}
85+
);
86+
87+
$registry = new WP_Sitemaps_Registry();
88+
$actual = $registry->add_provider( 'foo', $original_provider );
89+
90+
$this->assertTrue( $actual );
91+
$this->assertSame( $filtered_provider, $registry->get_provider( 'foo' ) );
92+
}
93+
94+
/**
95+
* Tests that add_provider() returns false when the wp_sitemaps_add_provider filter returns an invalid value.
96+
*
97+
* @covers WP_Sitemaps_Registry::add_provider
98+
*
99+
* @dataProvider data_invalid_sitemap_providers
100+
*
101+
* @param mixed $invalid_provider Invalid provider value.
102+
*/
103+
public function test_add_provider_filter_rejects_non_provider( $invalid_provider ) {
104+
add_filter(
105+
'wp_sitemaps_add_provider',
106+
static function () use ( $invalid_provider ) {
107+
return $invalid_provider;
108+
}
109+
);
110+
111+
$registry = new WP_Sitemaps_Registry();
112+
$actual = $registry->add_provider( 'foo', new WP_Sitemaps_Test_Provider() );
113+
114+
$this->assertFalse( $actual );
115+
$this->assertNull( $registry->get_provider( 'foo' ) );
116+
$this->assertSame( array(), $registry->get_providers() );
117+
}
118+
119+
/**
120+
* Data provider for {@see self::test_add_provider_filter_rejects_non_provider()}.
121+
*
122+
* @return array
123+
*/
124+
public function data_invalid_sitemap_providers() {
125+
return array(
126+
'null' => array( null ),
127+
'false' => array( false ),
128+
'true' => array( true ),
129+
'string' => array( 'invalid' ),
130+
'integer' => array( 123 ),
131+
'array' => array( array() ),
132+
'stdClass' => array( new stdClass() ),
133+
);
134+
}
135+
136+
/**
137+
* Tests that get_provider() returns null for unregistered provider names.
138+
*
139+
* @covers WP_Sitemaps_Registry::get_provider
140+
*/
141+
public function test_get_provider_unregistered_name() {
142+
$registry = new WP_Sitemaps_Registry();
143+
144+
$this->assertNull( $registry->get_provider( 'non_existent_provider' ) );
145+
}
146+
147+
/**
148+
* Tests that get_providers() returns an empty array when no providers are registered.
149+
*
150+
* @covers WP_Sitemaps_Registry::get_providers
151+
*/
152+
public function test_get_providers_empty() {
153+
$registry = new WP_Sitemaps_Registry();
154+
155+
$this->assertSame( array(), $registry->get_providers() );
156+
}
157+
158+
/**
159+
* Tests that get_providers() returns all registered providers keyed by name.
160+
*
161+
* @covers WP_Sitemaps_Registry::get_providers
162+
*/
163+
public function test_get_providers_multiple() {
164+
$provider_1 = new WP_Sitemaps_Test_Provider();
165+
$provider_2 = new WP_Sitemaps_Test_Provider();
166+
$registry = new WP_Sitemaps_Registry();
167+
168+
$registry->add_provider( 'foo', $provider_1 );
169+
$registry->add_provider( 'bar', $provider_2 );
170+
171+
$expected = array(
172+
'foo' => $provider_1,
173+
'bar' => $provider_2,
174+
);
175+
176+
$this->assertSame( $expected, $registry->get_providers() );
177+
}
70178
}

0 commit comments

Comments
 (0)