Merge "mw.rcfilters.ui.SaveFiltersPopupButtonWidget: Remove pointless option"
[lhc/web/wiklou.git] / tests / phpunit / includes / page / WikiPageDbTestBase.php
index 961b44e..2b803ae 100644 (file)
@@ -1422,4 +1422,277 @@ more stuff
                $this->assertNull( WikiPage::newFromID( 73574757437437743743 ) );
        }
 
+       public function provideTestInsertProtectNullRevision() {
+               // @codingStandardsIgnoreStart Generic.Files.LineLength
+               yield [
+                       'goat-message-key',
+                       [ 'edit' => 'sysop' ],
+                       [ 'edit' => '20200101040404' ],
+                       false,
+                       'Goat Reason',
+                       true,
+                       '(goat-message-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Reason(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04)))'
+               ];
+               yield [
+                       'goat-key',
+                       [ 'edit' => 'sysop', 'move' => 'something' ],
+                       [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
+                       false,
+                       'Goat Goat',
+                       true,
+                       '(goat-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Goat(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04))(word-separator)(protect-summary-desc: (restriction-move), (protect-level-something), (protect-expiring: 05:05, 1 (january) 2021, 1 (january) 2021, 05:05)))'
+               ];
+               // @codingStandardsIgnoreEnd Generic.Files.LineLength
+       }
+
+       /**
+        * @dataProvider provideTestInsertProtectNullRevision
+        * @covers WikiPage::insertProtectNullRevision
+        * @covers WikiPage::protectDescription
+        *
+        * @param string $revCommentMsg
+        * @param array $limit
+        * @param array $expiry
+        * @param bool $cascade
+        * @param string $reason
+        * @param bool|null $user true if the test sysop should be used, or null
+        * @param string $expectedComment
+        */
+       public function testInsertProtectNullRevision(
+               $revCommentMsg,
+               array $limit,
+               array $expiry,
+               $cascade,
+               $reason,
+               $user,
+               $expectedComment
+       ) {
+               $this->setContentLang( 'qqx' );
+
+               $page = $this->createPage( __METHOD__, 'Goat' );
+
+               $user = $user === null ? $user : $this->getTestSysop()->getUser();
+
+               $result = $page->insertProtectNullRevision(
+                       $revCommentMsg,
+                       $limit,
+                       $expiry,
+                       $cascade,
+                       $reason,
+                       $user
+               );
+
+               $this->assertTrue( $result instanceof Revision );
+               $this->assertSame( $expectedComment, $result->getComment( Revision::RAW ) );
+       }
+
+       /**
+        * @covers WikiPage::updateRevisionOn
+        */
+       public function testUpdateRevisionOn_existingPage() {
+               $user = $this->getTestSysop()->getUser();
+               $page = $this->createPage( __METHOD__, 'StartText' );
+
+               $revision = new Revision(
+                       [
+                               'id' => 9989,
+                               'page' => $page->getId(),
+                               'title' => $page->getTitle(),
+                               'comment' => __METHOD__,
+                               'minor_edit' => true,
+                               'text' => __METHOD__ . '-text',
+                               'len' => strlen( __METHOD__ . '-text' ),
+                               'user' => $user->getId(),
+                               'user_text' => $user->getName(),
+                               'timestamp' => '20170707040404',
+                               'content_model' => CONTENT_MODEL_WIKITEXT,
+                               'content_format' => CONTENT_FORMAT_WIKITEXT,
+                       ]
+               );
+
+               $result = $page->updateRevisionOn( $this->db, $revision );
+               $this->assertTrue( $result );
+               $this->assertSame( 9989, $page->getLatest() );
+               $this->assertEquals( $revision, $page->getRevision() );
+       }
+
+       /**
+        * @covers WikiPage::updateRevisionOn
+        */
+       public function testUpdateRevisionOn_NonExistingPage() {
+               $user = $this->getTestSysop()->getUser();
+               $page = $this->createPage( __METHOD__, 'StartText' );
+               $page->doDeleteArticle( 'reason' );
+
+               $revision = new Revision(
+                       [
+                               'id' => 9989,
+                               'page' => $page->getId(),
+                               'title' => $page->getTitle(),
+                               'comment' => __METHOD__,
+                               'minor_edit' => true,
+                               'text' => __METHOD__ . '-text',
+                               'len' => strlen( __METHOD__ . '-text' ),
+                               'user' => $user->getId(),
+                               'user_text' => $user->getName(),
+                               'timestamp' => '20170707040404',
+                               'content_model' => CONTENT_MODEL_WIKITEXT,
+                               'content_format' => CONTENT_FORMAT_WIKITEXT,
+                       ]
+               );
+
+               $result = $page->updateRevisionOn( $this->db, $revision );
+               $this->assertFalse( $result );
+       }
+
+       /**
+        * @covers WikiPage::updateIfNewerOn
+        */
+       public function testUpdateIfNewerOn_olderRevision() {
+               $user = $this->getTestSysop()->getUser();
+               $page = $this->createPage( __METHOD__, 'StartText' );
+               $initialRevision = $page->getRevision();
+
+               $olderTimeStamp = wfTimestamp(
+                       TS_MW,
+                       wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) - 1
+               );
+
+               $olderRevison = new Revision(
+                       [
+                               'id' => 9989,
+                               'page' => $page->getId(),
+                               'title' => $page->getTitle(),
+                               'comment' => __METHOD__,
+                               'minor_edit' => true,
+                               'text' => __METHOD__ . '-text',
+                               'len' => strlen( __METHOD__ . '-text' ),
+                               'user' => $user->getId(),
+                               'user_text' => $user->getName(),
+                               'timestamp' => $olderTimeStamp,
+                               'content_model' => CONTENT_MODEL_WIKITEXT,
+                               'content_format' => CONTENT_FORMAT_WIKITEXT,
+                       ]
+               );
+
+               $result = $page->updateIfNewerOn( $this->db, $olderRevison );
+               $this->assertFalse( $result );
+       }
+
+       /**
+        * @covers WikiPage::updateIfNewerOn
+        */
+       public function testUpdateIfNewerOn_newerRevision() {
+               $user = $this->getTestSysop()->getUser();
+               $page = $this->createPage( __METHOD__, 'StartText' );
+               $initialRevision = $page->getRevision();
+
+               $newerTimeStamp = wfTimestamp(
+                       TS_MW,
+                       wfTimestamp( TS_UNIX, $initialRevision->getTimestamp() ) + 1
+               );
+
+               $newerRevision = new Revision(
+                       [
+                               'id' => 9989,
+                               'page' => $page->getId(),
+                               'title' => $page->getTitle(),
+                               'comment' => __METHOD__,
+                               'minor_edit' => true,
+                               'text' => __METHOD__ . '-text',
+                               'len' => strlen( __METHOD__ . '-text' ),
+                               'user' => $user->getId(),
+                               'user_text' => $user->getName(),
+                               'timestamp' => $newerTimeStamp,
+                               'content_model' => CONTENT_MODEL_WIKITEXT,
+                               'content_format' => CONTENT_FORMAT_WIKITEXT,
+                       ]
+               );
+               $result = $page->updateIfNewerOn( $this->db, $newerRevision );
+               $this->assertTrue( $result );
+       }
+
+       /**
+        * @covers WikiPage::insertOn
+        */
+       public function testInsertOn() {
+               $title = Title::newFromText( __METHOD__ );
+               $page = new WikiPage( $title );
+
+               $startTimeStamp = wfTimestampNow();
+               $result = $page->insertOn( $this->db );
+               $endTimeStamp = wfTimestampNow();
+
+               $this->assertInternalType( 'int', $result );
+               $this->assertTrue( $result > 0 );
+
+               $condition = [ 'page_id' => $result ];
+
+               // Check the default fields have been filled
+               $this->assertSelect(
+                       'page',
+                       [
+                               'page_namespace',
+                               'page_title',
+                               'page_restrictions',
+                               'page_is_redirect',
+                               'page_is_new',
+                               'page_latest',
+                               'page_len',
+                       ],
+                       $condition,
+                       [ [
+                               '0',
+                               __METHOD__,
+                               '',
+                               '0',
+                               '1',
+                               '0',
+                               '0',
+                       ] ]
+               );
+
+               // Check the page_random field has been filled
+               $pageRandom = $this->db->selectField( 'page', 'page_random', $condition );
+               $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
+
+               // Assert the touched timestamp in the DB is roughly when we inserted the page
+               $pageTouched = $this->db->selectField( 'page', 'page_touched', $condition );
+               $this->assertTrue(
+                       wfTimestamp( TS_UNIX, $startTimeStamp )
+                       <= wfTimestamp( TS_UNIX, $pageTouched )
+               );
+               $this->assertTrue(
+                       wfTimestamp( TS_UNIX, $endTimeStamp )
+                       >= wfTimestamp( TS_UNIX, $pageTouched )
+               );
+
+               // Try inserting the same page again and checking the result is false (no change)
+               $result = $page->insertOn( $this->db );
+               $this->assertFalse( $result );
+       }
+
+       /**
+        * @covers WikiPage::insertOn
+        */
+       public function testInsertOn_idSpecified() {
+               $title = Title::newFromText( __METHOD__ );
+               $page = new WikiPage( $title );
+               $id = 1478952189;
+
+               $result = $page->insertOn( $this->db, $id );
+
+               $this->assertSame( $id, $result );
+
+               $condition = [ 'page_id' => $result ];
+
+               // Check there is actually a row in the db
+               $this->assertSelect(
+                       'page',
+                       [ 'page_title' ],
+                       $condition,
+                       [ [ __METHOD__ ] ]
+               );
+       }
+
 }