It should not be possible for a RequestContext's WikiPage and Title to be different.
authorDaniel Friesen <daniel@nadir-seen-fire.com>
Wed, 24 Oct 2012 14:32:46 +0000 (07:32 -0700)
committerDaniel Friesen <daniel@nadir-seen-fire.com>
Wed, 24 Oct 2012 15:17:27 +0000 (08:17 -0700)
Looks like someone messed with RequestContext when I wasn't looking at it.
WikiPage is a representation of the Title. It should not be possible for wikipage
to point to a different title than the context's title.

Fix this issue by unsetting WikiPage when setting title and updating title when
setting wikipage.

Change-Id: I40471b12d08763cb1b47b8382f96d8db94b4f319

includes/context/RequestContext.php
tests/phpunit/includes/RequestContextTest.php [new file with mode: 0644]

index cd2bf55..e4de030 100644 (file)
@@ -93,6 +93,8 @@ class RequestContext implements IContextSource {
         */
        public function setTitle( Title $t ) {
                $this->title = $t;
+               // Erase the WikiPage so a new one with the new title gets created.
+               $this->wikipage = null;
        }
 
        /**
@@ -138,6 +140,12 @@ class RequestContext implements IContextSource {
         * @param $p WikiPage object
         */
        public function setWikiPage( WikiPage $p ) {
+               $contextTitle = $this->getTitle();
+               $pageTitle = $p->getTitle();
+               if ( !$contextTitle || !$pageTitle->equals( $contextTitle ) ) {
+                       $this->setTitle( $pageTitle );
+               }
+               // Defer this to the end since setTitle sets it to null.
                $this->wikipage = $p;
        }
 
diff --git a/tests/phpunit/includes/RequestContextTest.php b/tests/phpunit/includes/RequestContextTest.php
new file mode 100644 (file)
index 0000000..48cf6dc
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+class RequestContextTest extends MediaWikiTestCase {
+
+       /**
+        * Test the relationship between title and wikipage in RequestContext
+        */
+       public function testWikiPageTitle() {
+               $context = new RequestContext();
+
+               $curTitle = Title::newFromText( "A" );
+               $context->setTitle( $curTitle );
+               $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
+                       "When a title is first set WikiPage should be created on-demand for that title." );
+
+               $curTitle = Title::newFromText( "B" );
+               $context->setWikiPage( WikiPage::factory( $curTitle ) );
+               $this->assertTrue( $curTitle->equals( $context->getTitle() ),
+                       "Title must be updated when a new WikiPage is provided." );
+
+               $curTitle = Title::newFromText( "C" );
+               $context->setTitle( $curTitle );
+               $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
+                       "When a title is updated the WikiPage should be purged and recreated on-demand with the new title." );
+
+       }
+
+}