7 * ^--- tell jenkins this test needs the database
10 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
12 class PagePropsTest
extends MediaWikiLangTestCase
{
25 * @var array $the_properties
27 private $the_properties;
29 protected function setUp() {
30 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
34 $wgExtraNamespaces[12312] = 'Dummy';
35 $wgExtraNamespaces[12313] = 'Dummy_talk';
37 $wgNamespaceContentModels[12312] = 'DUMMY';
38 $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
40 MWNamespace
::clearCaches();
41 $wgContLang->resetNamespaces(); # reset namespace cache
43 if ( !$this->the_properties
) {
44 $this->the_properties
= [
45 "property1" => "value1",
46 "property2" => "value2",
47 "property3" => "value3",
48 "property4" => "value4"
52 if ( !$this->title1
) {
53 $page = $this->createPage(
54 'PagePropsTest_page_1',
56 CONTENT_MODEL_WIKITEXT
58 $this->title1
= $page->getTitle();
59 $page1ID = $this->title1
->getArticleID();
60 $this->setProperties( $page1ID, $this->the_properties
);
63 if ( !$this->title2
) {
64 $page = $this->createPage(
65 'PagePropsTest_page_2',
67 CONTENT_MODEL_WIKITEXT
69 $this->title2
= $page->getTitle();
70 $page2ID = $this->title2
->getArticleID();
71 $this->setProperties( $page2ID, $this->the_properties
);
75 protected function tearDown() {
76 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
80 unset( $wgExtraNamespaces[12312] );
81 unset( $wgExtraNamespaces[12313] );
83 unset( $wgNamespaceContentModels[12312] );
84 unset( $wgContentHandlers['DUMMY'] );
86 MWNamespace
::clearCaches();
87 $wgContLang->resetNamespaces(); # reset namespace cache
91 * Test getting a single property from a single page. The property was
94 public function testGetSingleProperty() {
95 $pageProps = PageProps
::getInstance();
96 $page1ID = $this->title1
->getArticleID();
97 $result = $pageProps->getProperties( $this->title1
, "property1" );
98 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
99 $this->assertEquals( $result[$page1ID], "value1", "Get property" );
103 * Test getting a single property from multiple pages. The property was
106 public function testGetSinglePropertyMultiplePages() {
107 $pageProps = PageProps
::getInstance();
108 $page1ID = $this->title1
->getArticleID();
109 $page2ID = $this->title2
->getArticleID();
114 $result = $pageProps->getProperties( $titles, "property1" );
115 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
116 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
117 $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
118 $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
122 * Test getting multiple properties from multiple pages. The properties
123 * were set in setUp().
125 public function testGetMultiplePropertiesMultiplePages() {
126 $pageProps = PageProps
::getInstance();
127 $page1ID = $this->title1
->getArticleID();
128 $page2ID = $this->title2
->getArticleID();
137 $result = $pageProps->getProperties( $titles, $properties );
138 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
139 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
140 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
141 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
142 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
143 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
144 $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
145 $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
146 $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
147 $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
151 * Test getting all properties from a single page. The properties were
152 * set in setUp(). The properties retrieved from the page may include
153 * additional properties not set in the test case that are added by
154 * other extensions. Therefore, rather than checking to see if the
155 * properties that were set in the test case exactly match the
156 * retrieved properties, we need to check to see if they are a
157 * subset of the retrieved properties. Since this version of PHPUnit
158 * does not yet include assertArraySubset(), we needed to code the
159 * equivalent functionality.
161 public function testGetAllProperties() {
162 $pageProps = PageProps
::getInstance();
163 $page1ID = $this->title1
->getArticleID();
164 $result = $pageProps->getAllProperties( $this->title1
);
165 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
166 $properties = $result[$page1ID];
167 $patched = array_replace_recursive( $properties, $this->the_properties
);
168 $this->assertEquals( $patched, $properties, "Get all properties" );
172 * Test getting all properties from multiple pages. The properties were
173 * set in setUp(). See getAllProperties() above for more information.
175 public function testGetAllPropertiesMultiplePages() {
176 $pageProps = PageProps
::getInstance();
177 $page1ID = $this->title1
->getArticleID();
178 $page2ID = $this->title2
->getArticleID();
183 $result = $pageProps->getAllProperties( $titles );
184 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
185 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
186 $properties1 = $result[$page1ID];
187 $patched = array_replace_recursive( $properties1, $this->the_properties
);
188 $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
189 $properties2 = $result[$page2ID];
190 $patched = array_replace_recursive( $properties2, $this->the_properties
);
191 $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
195 * Test caching when retrieving single properties by getting a property,
196 * saving a new value for the property, then getting the property
197 * again. The cached value for the property rather than the new value
198 * of the property should be returned.
200 public function testSingleCache() {
201 $pageProps = PageProps
::getInstance();
202 $page1ID = $this->title1
->getArticleID();
203 $value1 = $pageProps->getProperties( $this->title1
, "property1" );
204 $this->setProperty( $page1ID, "property1", "another value" );
205 $value2 = $pageProps->getProperties( $this->title1
, "property1" );
206 $this->assertEquals( $value1, $value2, "Single cache" );
210 * Test caching when retrieving all properties by getting all
211 * properties, saving a new value for a property, then getting all
212 * properties again. The cached value for the properties rather than the
213 * new value of the properties should be returned.
215 public function testMultiCache() {
216 $pageProps = PageProps
::getInstance();
217 $page1ID = $this->title1
->getArticleID();
218 $properties1 = $pageProps->getAllProperties( $this->title1
);
219 $this->setProperty( $page1ID, "property1", "another value" );
220 $properties2 = $pageProps->getAllProperties( $this->title1
);
221 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
225 * Test that getting all properties clears the single properties
226 * that have been cached by getting a property, saving a new value for
227 * the property, getting all properties (which clears the cached single
228 * properties), then getting the property again. The new value for the
229 * property rather than the cached value of the property should be
232 public function testClearCache() {
233 $pageProps = PageProps
::getInstance();
234 $page1ID = $this->title1
->getArticleID();
235 $pageProps->getProperties( $this->title1
, "property1" );
236 $new_value = "another value";
237 $this->setProperty( $page1ID, "property1", $new_value );
238 $pageProps->getAllProperties( $this->title1
);
239 $result = $pageProps->getProperties( $this->title1
, "property1" );
240 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
241 $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
244 protected function createPage( $page, $text, $model = null ) {
245 if ( is_string( $page ) ) {
246 if ( !preg_match( '/:/', $page ) &&
247 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
249 $ns = $this->getDefaultWikitextNS();
250 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
253 $page = Title
::newFromText( $page );
256 if ( $page instanceof Title
) {
257 $page = new WikiPage( $page );
260 if ( $page->exists() ) {
261 $page->doDeleteArticle( "done" );
264 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
265 $page->doEditContent( $content, "testing", EDIT_NEW
);
270 protected function setProperties( $pageID, $properties ) {
273 foreach ( $properties as $propertyName => $propertyValue ) {
275 'pp_page' => $pageID,
276 'pp_propname' => $propertyName,
277 'pp_value' => $propertyValue
283 $dbw = wfGetDB( DB_MASTER
);
297 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
299 $properties[$propertyName] = $propertyValue;
301 $this->setProperties( $pageID, $properties );