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() {
32 $this->setMwGlobals( [
33 'wgExtraNamespaces' => [
35 12313 => 'Dummy_talk',
37 'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
40 $this->mergeMwGlobalArrayValue(
42 [ 'DUMMY' => 'DummyContentHandlerForTesting' ]
45 if ( !$this->the_properties
) {
46 $this->the_properties
= [
47 "property1" => "value1",
48 "property2" => "value2",
49 "property3" => "value3",
50 "property4" => "value4"
54 if ( !$this->title1
) {
55 $page = $this->createPage(
56 'PagePropsTest_page_1',
58 CONTENT_MODEL_WIKITEXT
60 $this->title1
= $page->getTitle();
61 $page1ID = $this->title1
->getArticleID();
62 $this->setProperties( $page1ID, $this->the_properties
);
65 if ( !$this->title2
) {
66 $page = $this->createPage(
67 'PagePropsTest_page_2',
69 CONTENT_MODEL_WIKITEXT
71 $this->title2
= $page->getTitle();
72 $page2ID = $this->title2
->getArticleID();
73 $this->setProperties( $page2ID, $this->the_properties
);
78 * Test getting a single property from a single page. The property was
81 public function testGetSingleProperty() {
82 $pageProps = PageProps
::getInstance();
83 $page1ID = $this->title1
->getArticleID();
84 $result = $pageProps->getProperties( $this->title1
, "property1" );
85 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
86 $this->assertEquals( $result[$page1ID], "value1", "Get property" );
90 * Test getting a single property from multiple pages. The property was
93 public function testGetSinglePropertyMultiplePages() {
94 $pageProps = PageProps
::getInstance();
95 $page1ID = $this->title1
->getArticleID();
96 $page2ID = $this->title2
->getArticleID();
101 $result = $pageProps->getProperties( $titles, "property1" );
102 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
103 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
104 $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
105 $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
109 * Test getting multiple properties from multiple pages. The properties
110 * were set in setUp().
112 public function testGetMultiplePropertiesMultiplePages() {
113 $pageProps = PageProps
::getInstance();
114 $page1ID = $this->title1
->getArticleID();
115 $page2ID = $this->title2
->getArticleID();
124 $result = $pageProps->getProperties( $titles, $properties );
125 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
126 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
127 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
128 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
129 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
130 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
131 $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
132 $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
133 $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
134 $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
138 * Test getting all properties from a single page. The properties were
139 * set in setUp(). The properties retrieved from the page may include
140 * additional properties not set in the test case that are added by
141 * other extensions. Therefore, rather than checking to see if the
142 * properties that were set in the test case exactly match the
143 * retrieved properties, we need to check to see if they are a
144 * subset of the retrieved properties. Since this version of PHPUnit
145 * does not yet include assertArraySubset(), we needed to code the
146 * equivalent functionality.
148 public function testGetAllProperties() {
149 $pageProps = PageProps
::getInstance();
150 $page1ID = $this->title1
->getArticleID();
151 $result = $pageProps->getAllProperties( $this->title1
);
152 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
153 $properties = $result[$page1ID];
154 $patched = array_replace_recursive( $properties, $this->the_properties
);
155 $this->assertEquals( $patched, $properties, "Get all properties" );
159 * Test getting all properties from multiple pages. The properties were
160 * set in setUp(). See getAllProperties() above for more information.
162 public function testGetAllPropertiesMultiplePages() {
163 $pageProps = PageProps
::getInstance();
164 $page1ID = $this->title1
->getArticleID();
165 $page2ID = $this->title2
->getArticleID();
170 $result = $pageProps->getAllProperties( $titles );
171 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
172 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
173 $properties1 = $result[$page1ID];
174 $patched = array_replace_recursive( $properties1, $this->the_properties
);
175 $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
176 $properties2 = $result[$page2ID];
177 $patched = array_replace_recursive( $properties2, $this->the_properties
);
178 $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
182 * Test caching when retrieving single properties by getting a property,
183 * saving a new value for the property, then getting the property
184 * again. The cached value for the property rather than the new value
185 * of the property should be returned.
187 public function testSingleCache() {
188 $pageProps = PageProps
::getInstance();
189 $page1ID = $this->title1
->getArticleID();
190 $value1 = $pageProps->getProperties( $this->title1
, "property1" );
191 $this->setProperty( $page1ID, "property1", "another value" );
192 $value2 = $pageProps->getProperties( $this->title1
, "property1" );
193 $this->assertEquals( $value1, $value2, "Single cache" );
197 * Test caching when retrieving all properties by getting all
198 * properties, saving a new value for a property, then getting all
199 * properties again. The cached value for the properties rather than the
200 * new value of the properties should be returned.
202 public function testMultiCache() {
203 $pageProps = PageProps
::getInstance();
204 $page1ID = $this->title1
->getArticleID();
205 $properties1 = $pageProps->getAllProperties( $this->title1
);
206 $this->setProperty( $page1ID, "property1", "another value" );
207 $properties2 = $pageProps->getAllProperties( $this->title1
);
208 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
212 * Test that getting all properties clears the single properties
213 * that have been cached by getting a property, saving a new value for
214 * the property, getting all properties (which clears the cached single
215 * properties), then getting the property again. The new value for the
216 * property rather than the cached value of the property should be
219 public function testClearCache() {
220 $pageProps = PageProps
::getInstance();
221 $page1ID = $this->title1
->getArticleID();
222 $pageProps->getProperties( $this->title1
, "property1" );
223 $new_value = "another value";
224 $this->setProperty( $page1ID, "property1", $new_value );
225 $pageProps->getAllProperties( $this->title1
);
226 $result = $pageProps->getProperties( $this->title1
, "property1" );
227 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
228 $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
231 protected function createPage( $page, $text, $model = null ) {
232 if ( is_string( $page ) ) {
233 if ( !preg_match( '/:/', $page ) &&
234 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
236 $ns = $this->getDefaultWikitextNS();
237 $page = MWNamespace
::getCanonicalName( $ns ) . ':' . $page;
240 $page = Title
::newFromText( $page );
243 if ( $page instanceof Title
) {
244 $page = new WikiPage( $page );
247 if ( $page->exists() ) {
248 $page->doDeleteArticle( "done" );
251 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
252 $page->doEditContent( $content, "testing", EDIT_NEW
);
257 protected function setProperties( $pageID, $properties ) {
260 foreach ( $properties as $propertyName => $propertyValue ) {
262 'pp_page' => $pageID,
263 'pp_propname' => $propertyName,
264 'pp_value' => $propertyValue
270 $dbw = wfGetDB( DB_MASTER
);
284 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
286 $properties[$propertyName] = $propertyValue;
288 $this->setProperties( $pageID, $properties );