3 use MediaWiki\MediaWikiServices
;
9 * ^--- tell jenkins this test needs the database
12 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
14 class PagePropsTest
extends MediaWikiLangTestCase
{
27 * @var array $the_properties
29 private $the_properties;
31 protected function setUp() {
34 $this->setMwGlobals( [
35 'wgExtraNamespaces' => [
37 12313 => 'Dummy_talk',
39 'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
42 $this->mergeMwGlobalArrayValue(
44 [ 'DUMMY' => 'DummyContentHandlerForTesting' ]
47 if ( !$this->the_properties
) {
48 $this->the_properties
= [
49 "property1" => "value1",
50 "property2" => "value2",
51 "property3" => "value3",
52 "property4" => "value4"
56 if ( !$this->title1
) {
57 $page = $this->createPage(
58 'PagePropsTest_page_1',
60 CONTENT_MODEL_WIKITEXT
62 $this->title1
= $page->getTitle();
63 $page1ID = $this->title1
->getArticleID();
64 $this->setProperties( $page1ID, $this->the_properties
);
67 if ( !$this->title2
) {
68 $page = $this->createPage(
69 'PagePropsTest_page_2',
71 CONTENT_MODEL_WIKITEXT
73 $this->title2
= $page->getTitle();
74 $page2ID = $this->title2
->getArticleID();
75 $this->setProperties( $page2ID, $this->the_properties
);
80 * Test getting a single property from a single page. The property was
83 public function testGetSingleProperty() {
84 $pageProps = PageProps
::getInstance();
85 $page1ID = $this->title1
->getArticleID();
86 $result = $pageProps->getProperties( $this->title1
, "property1" );
87 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
88 $this->assertEquals( $result[$page1ID], "value1", "Get property" );
92 * Test getting a single property from multiple pages. The property was
95 public function testGetSinglePropertyMultiplePages() {
96 $pageProps = PageProps
::getInstance();
97 $page1ID = $this->title1
->getArticleID();
98 $page2ID = $this->title2
->getArticleID();
103 $result = $pageProps->getProperties( $titles, "property1" );
104 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
105 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
106 $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
107 $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
111 * Test getting multiple properties from multiple pages. The properties
112 * were set in setUp().
114 public function testGetMultiplePropertiesMultiplePages() {
115 $pageProps = PageProps
::getInstance();
116 $page1ID = $this->title1
->getArticleID();
117 $page2ID = $this->title2
->getArticleID();
126 $result = $pageProps->getProperties( $titles, $properties );
127 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
128 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
129 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
130 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
131 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
132 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
133 $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
134 $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
135 $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
136 $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
140 * Test getting all properties from a single page. The properties were
141 * set in setUp(). The properties retrieved from the page may include
142 * additional properties not set in the test case that are added by
143 * other extensions. Therefore, rather than checking to see if the
144 * properties that were set in the test case exactly match the
145 * retrieved properties, we need to check to see if they are a
146 * subset of the retrieved properties. Since this version of PHPUnit
147 * does not yet include assertArraySubset(), we needed to code the
148 * equivalent functionality.
150 public function testGetAllProperties() {
151 $pageProps = PageProps
::getInstance();
152 $page1ID = $this->title1
->getArticleID();
153 $result = $pageProps->getAllProperties( $this->title1
);
154 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
155 $properties = $result[$page1ID];
156 $patched = array_replace_recursive( $properties, $this->the_properties
);
157 $this->assertEquals( $patched, $properties, "Get all properties" );
161 * Test getting all properties from multiple pages. The properties were
162 * set in setUp(). See getAllProperties() above for more information.
164 public function testGetAllPropertiesMultiplePages() {
165 $pageProps = PageProps
::getInstance();
166 $page1ID = $this->title1
->getArticleID();
167 $page2ID = $this->title2
->getArticleID();
172 $result = $pageProps->getAllProperties( $titles );
173 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
174 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
175 $properties1 = $result[$page1ID];
176 $patched = array_replace_recursive( $properties1, $this->the_properties
);
177 $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
178 $properties2 = $result[$page2ID];
179 $patched = array_replace_recursive( $properties2, $this->the_properties
);
180 $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
184 * Test caching when retrieving single properties by getting a property,
185 * saving a new value for the property, then getting the property
186 * again. The cached value for the property rather than the new value
187 * of the property should be returned.
189 public function testSingleCache() {
190 $pageProps = PageProps
::getInstance();
191 $page1ID = $this->title1
->getArticleID();
192 $value1 = $pageProps->getProperties( $this->title1
, "property1" );
193 $this->setProperty( $page1ID, "property1", "another value" );
194 $value2 = $pageProps->getProperties( $this->title1
, "property1" );
195 $this->assertEquals( $value1, $value2, "Single cache" );
199 * Test caching when retrieving all properties by getting all
200 * properties, saving a new value for a property, then getting all
201 * properties again. The cached value for the properties rather than the
202 * new value of the properties should be returned.
204 public function testMultiCache() {
205 $pageProps = PageProps
::getInstance();
206 $page1ID = $this->title1
->getArticleID();
207 $properties1 = $pageProps->getAllProperties( $this->title1
);
208 $this->setProperty( $page1ID, "property1", "another value" );
209 $properties2 = $pageProps->getAllProperties( $this->title1
);
210 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
214 * Test that getting all properties clears the single properties
215 * that have been cached by getting a property, saving a new value for
216 * the property, getting all properties (which clears the cached single
217 * properties), then getting the property again. The new value for the
218 * property rather than the cached value of the property should be
221 public function testClearCache() {
222 $pageProps = PageProps
::getInstance();
223 $page1ID = $this->title1
->getArticleID();
224 $pageProps->getProperties( $this->title1
, "property1" );
225 $new_value = "another value";
226 $this->setProperty( $page1ID, "property1", $new_value );
227 $pageProps->getAllProperties( $this->title1
);
228 $result = $pageProps->getProperties( $this->title1
, "property1" );
229 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
230 $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
233 protected function createPage( $page, $text, $model = null ) {
234 if ( is_string( $page ) ) {
235 if ( !preg_match( '/:/', $page ) &&
236 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
238 $ns = $this->getDefaultWikitextNS();
239 $page = MediaWikiServices
::getInstance()->getNamespaceInfo()->
240 getCanonicalName( $ns ) . ':' . $page;
243 $page = Title
::newFromText( $page );
246 if ( $page instanceof Title
) {
247 $page = new WikiPage( $page );
250 if ( $page->exists() ) {
251 $page->doDeleteArticle( "done" );
254 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
255 $page->doEditContent( $content, "testing", EDIT_NEW
);
260 protected function setProperties( $pageID, $properties ) {
263 foreach ( $properties as $propertyName => $propertyValue ) {
265 'pp_page' => $pageID,
266 'pp_propname' => $propertyName,
267 'pp_value' => $propertyValue
273 $dbw = wfGetDB( DB_MASTER
);
287 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
289 $properties[$propertyName] = $propertyValue;
291 $this->setProperties( $pageID, $properties );