PagePropsTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @covers PageProps
  4. *
  5. * @group Database
  6. * ^--- tell jenkins this test needs the database
  7. *
  8. * @group medium
  9. * ^--- tell phpunit that these test cases may take longer than 2 seconds.
  10. */
  11. class PagePropsTest extends MediaWikiLangTestCase {
  12. /**
  13. * @var Title $title1
  14. */
  15. private $title1;
  16. /**
  17. * @var Title $title2
  18. */
  19. private $title2;
  20. /**
  21. * @var array $the_properties
  22. */
  23. private $the_properties;
  24. protected function setUp() {
  25. parent::setUp();
  26. $this->setMwGlobals( [
  27. 'wgExtraNamespaces' => [
  28. 12312 => 'Dummy',
  29. 12313 => 'Dummy_talk',
  30. ],
  31. 'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
  32. ] );
  33. $this->mergeMwGlobalArrayValue(
  34. 'wgContentHandlers',
  35. [ 'DUMMY' => 'DummyContentHandlerForTesting' ]
  36. );
  37. if ( !$this->the_properties ) {
  38. $this->the_properties = [
  39. "property1" => "value1",
  40. "property2" => "value2",
  41. "property3" => "value3",
  42. "property4" => "value4"
  43. ];
  44. }
  45. if ( !$this->title1 ) {
  46. $page = $this->createPage(
  47. 'PagePropsTest_page_1',
  48. "just a dummy page",
  49. CONTENT_MODEL_WIKITEXT
  50. );
  51. $this->title1 = $page->getTitle();
  52. $page1ID = $this->title1->getArticleID();
  53. $this->setProperties( $page1ID, $this->the_properties );
  54. }
  55. if ( !$this->title2 ) {
  56. $page = $this->createPage(
  57. 'PagePropsTest_page_2',
  58. "just a dummy page",
  59. CONTENT_MODEL_WIKITEXT
  60. );
  61. $this->title2 = $page->getTitle();
  62. $page2ID = $this->title2->getArticleID();
  63. $this->setProperties( $page2ID, $this->the_properties );
  64. }
  65. }
  66. /**
  67. * Test getting a single property from a single page. The property was
  68. * set in setUp().
  69. */
  70. public function testGetSingleProperty() {
  71. $pageProps = PageProps::getInstance();
  72. $page1ID = $this->title1->getArticleID();
  73. $result = $pageProps->getProperties( $this->title1, "property1" );
  74. $this->assertArrayHasKey( $page1ID, $result, "Found property" );
  75. $this->assertEquals( $result[$page1ID], "value1", "Get property" );
  76. }
  77. /**
  78. * Test getting a single property from multiple pages. The property was
  79. * set in setUp().
  80. */
  81. public function testGetSinglePropertyMultiplePages() {
  82. $pageProps = PageProps::getInstance();
  83. $page1ID = $this->title1->getArticleID();
  84. $page2ID = $this->title2->getArticleID();
  85. $titles = [
  86. $this->title1,
  87. $this->title2
  88. ];
  89. $result = $pageProps->getProperties( $titles, "property1" );
  90. $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
  91. $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
  92. $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
  93. $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
  94. }
  95. /**
  96. * Test getting multiple properties from multiple pages. The properties
  97. * were set in setUp().
  98. */
  99. public function testGetMultiplePropertiesMultiplePages() {
  100. $pageProps = PageProps::getInstance();
  101. $page1ID = $this->title1->getArticleID();
  102. $page2ID = $this->title2->getArticleID();
  103. $titles = [
  104. $this->title1,
  105. $this->title2
  106. ];
  107. $properties = [
  108. "property1",
  109. "property2"
  110. ];
  111. $result = $pageProps->getProperties( $titles, $properties );
  112. $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
  113. $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
  114. $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
  115. $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
  116. $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
  117. $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
  118. $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
  119. $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
  120. $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
  121. $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
  122. }
  123. /**
  124. * Test getting all properties from a single page. The properties were
  125. * set in setUp(). The properties retrieved from the page may include
  126. * additional properties not set in the test case that are added by
  127. * other extensions. Therefore, rather than checking to see if the
  128. * properties that were set in the test case exactly match the
  129. * retrieved properties, we need to check to see if they are a
  130. * subset of the retrieved properties. Since this version of PHPUnit
  131. * does not yet include assertArraySubset(), we needed to code the
  132. * equivalent functionality.
  133. */
  134. public function testGetAllProperties() {
  135. $pageProps = PageProps::getInstance();
  136. $page1ID = $this->title1->getArticleID();
  137. $result = $pageProps->getAllProperties( $this->title1 );
  138. $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
  139. $properties = $result[$page1ID];
  140. $patched = array_replace_recursive( $properties, $this->the_properties );
  141. $this->assertEquals( $patched, $properties, "Get all properties" );
  142. }
  143. /**
  144. * Test getting all properties from multiple pages. The properties were
  145. * set in setUp(). See getAllProperties() above for more information.
  146. */
  147. public function testGetAllPropertiesMultiplePages() {
  148. $pageProps = PageProps::getInstance();
  149. $page1ID = $this->title1->getArticleID();
  150. $page2ID = $this->title2->getArticleID();
  151. $titles = [
  152. $this->title1,
  153. $this->title2
  154. ];
  155. $result = $pageProps->getAllProperties( $titles );
  156. $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
  157. $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
  158. $properties1 = $result[$page1ID];
  159. $patched = array_replace_recursive( $properties1, $this->the_properties );
  160. $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
  161. $properties2 = $result[$page2ID];
  162. $patched = array_replace_recursive( $properties2, $this->the_properties );
  163. $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
  164. }
  165. /**
  166. * Test caching when retrieving single properties by getting a property,
  167. * saving a new value for the property, then getting the property
  168. * again. The cached value for the property rather than the new value
  169. * of the property should be returned.
  170. */
  171. public function testSingleCache() {
  172. $pageProps = PageProps::getInstance();
  173. $page1ID = $this->title1->getArticleID();
  174. $value1 = $pageProps->getProperties( $this->title1, "property1" );
  175. $this->setProperty( $page1ID, "property1", "another value" );
  176. $value2 = $pageProps->getProperties( $this->title1, "property1" );
  177. $this->assertEquals( $value1, $value2, "Single cache" );
  178. }
  179. /**
  180. * Test caching when retrieving all properties by getting all
  181. * properties, saving a new value for a property, then getting all
  182. * properties again. The cached value for the properties rather than the
  183. * new value of the properties should be returned.
  184. */
  185. public function testMultiCache() {
  186. $pageProps = PageProps::getInstance();
  187. $page1ID = $this->title1->getArticleID();
  188. $properties1 = $pageProps->getAllProperties( $this->title1 );
  189. $this->setProperty( $page1ID, "property1", "another value" );
  190. $properties2 = $pageProps->getAllProperties( $this->title1 );
  191. $this->assertEquals( $properties1, $properties2, "Multi Cache" );
  192. }
  193. /**
  194. * Test that getting all properties clears the single properties
  195. * that have been cached by getting a property, saving a new value for
  196. * the property, getting all properties (which clears the cached single
  197. * properties), then getting the property again. The new value for the
  198. * property rather than the cached value of the property should be
  199. * returned.
  200. */
  201. public function testClearCache() {
  202. $pageProps = PageProps::getInstance();
  203. $page1ID = $this->title1->getArticleID();
  204. $pageProps->getProperties( $this->title1, "property1" );
  205. $new_value = "another value";
  206. $this->setProperty( $page1ID, "property1", $new_value );
  207. $pageProps->getAllProperties( $this->title1 );
  208. $result = $pageProps->getProperties( $this->title1, "property1" );
  209. $this->assertArrayHasKey( $page1ID, $result, "Found property" );
  210. $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
  211. }
  212. protected function createPage( $page, $text, $model = null ) {
  213. if ( is_string( $page ) ) {
  214. if ( !preg_match( '/:/', $page ) &&
  215. ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
  216. ) {
  217. $ns = $this->getDefaultWikitextNS();
  218. $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
  219. }
  220. $page = Title::newFromText( $page );
  221. }
  222. if ( $page instanceof Title ) {
  223. $page = new WikiPage( $page );
  224. }
  225. if ( $page->exists() ) {
  226. $page->doDeleteArticle( "done" );
  227. }
  228. $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
  229. $page->doEditContent( $content, "testing", EDIT_NEW );
  230. return $page;
  231. }
  232. protected function setProperties( $pageID, $properties ) {
  233. $rows = [];
  234. foreach ( $properties as $propertyName => $propertyValue ) {
  235. $row = [
  236. 'pp_page' => $pageID,
  237. 'pp_propname' => $propertyName,
  238. 'pp_value' => $propertyValue
  239. ];
  240. $rows[] = $row;
  241. }
  242. $dbw = wfGetDB( DB_MASTER );
  243. $dbw->replace(
  244. 'page_props',
  245. [
  246. [
  247. 'pp_page',
  248. 'pp_propname'
  249. ]
  250. ],
  251. $rows,
  252. __METHOD__
  253. );
  254. }
  255. protected function setProperty( $pageID, $propertyName, $propertyValue ) {
  256. $properties = [];
  257. $properties[$propertyName] = $propertyValue;
  258. $this->setProperties( $pageID, $properties );
  259. }
  260. }