From 05c7a34fa6eab25e52b9d572006d0312155186b8 Mon Sep 17 00:00:00 2001 From: Dreamy Jazz Date: Sun, 11 Aug 2024 16:44:49 +0200 Subject: [PATCH] Test fixUserRegistration.php Why: * Maintenance scripts in core are mostly untested and testing them will help to avoid regressions. What: * Create FixUserRegistrationTest that fully tests the associated maintenance script. Bug: T371167 Change-Id: Ifb3594f7dc0055568431002a9180df7db4da9692 --- .../maintenance/FixUserRegistrationTest.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/phpunit/maintenance/FixUserRegistrationTest.php diff --git a/tests/phpunit/maintenance/FixUserRegistrationTest.php b/tests/phpunit/maintenance/FixUserRegistrationTest.php new file mode 100644 index 00000000000..2f60ed152af --- /dev/null +++ b/tests/phpunit/maintenance/FixUserRegistrationTest.php @@ -0,0 +1,71 @@ +getMutableTestUser()->getUser(); + $this->getDb()->newUpdateQueryBuilder() + ->update( 'user' ) + ->set( [ 'user_registration' => $this->getDb()->timestamp( '20220405060708' ) ] ) + ->where( [ 'user_id' => $userWithValidRegistration->getId() ] ) + ->execute(); + ConvertibleTimestamp::setFakeTime( '20230405060708' ); + $userWithNullRegistrationButNoEdits = $this->getMutableTestUser()->getUser(); + $userWithNullRegistrationAndEdits = $this->getMutableTestUser()->getUser(); + $this->getDb()->newUpdateQueryBuilder() + ->update( 'user' ) + ->set( [ 'user_registration' => null ] ) + ->where( [ 'user_id' => [ + $userWithNullRegistrationButNoEdits->getId(), $userWithNullRegistrationAndEdits->getId() + ] ] ) + ->execute(); + // Make a testing edit for the $userWithNullRegistrationAndEdits + ConvertibleTimestamp::setFakeTime( '20230505060708' ); + $this->editPage( + Title::newFromText( 'Test' ), "testcontent", '', + NS_MAIN, $userWithNullRegistrationAndEdits + ); + ConvertibleTimestamp::setFakeTime( false ); + // Verify that the user_registration column is set up correctly for the test. + $expectedRows = [ + [ $userWithValidRegistration->getId(), $this->getDb()->timestamp( '20220405060708' ) ], + [ $userWithNullRegistrationButNoEdits->getId(), null ], + [ $userWithNullRegistrationAndEdits->getId(), null ], + ]; + $this->newSelectQueryBuilder() + ->select( [ 'user_id', 'user_registration' ] ) + ->from( 'user' ) + ->orderBy( 'user_id' ) + ->assertResultSet( $expectedRows ); + // Run the maintenance script + $this->maintenance->execute(); + $expectedOutputRegex = '/Could not find registration for #2[\s\S]*Set registration for #3 to ' . + preg_quote( $this->getDb()->timestamp( '20230505060708' ), '/' ) . '/'; + $this->expectOutputRegex( $expectedOutputRegex ); + $expectedRows = [ + [ $userWithValidRegistration->getId(), $this->getDb()->timestamp( '20220405060708' ) ], + [ $userWithNullRegistrationButNoEdits->getId(), null ], + [ $userWithNullRegistrationAndEdits->getId(), $this->getDb()->timestamp( '20230505060708' ) ], + ]; + $this->newSelectQueryBuilder() + ->select( [ 'user_id', 'user_registration' ] ) + ->from( 'user' ) + ->orderBy( 'user_id' ) + ->assertResultSet( $expectedRows ); + } +}