diff options
Diffstat (limited to 'www/wiki/extensions/Translate/tests/phpunit/TranslateSandboxTest.php')
-rw-r--r-- | www/wiki/extensions/Translate/tests/phpunit/TranslateSandboxTest.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/www/wiki/extensions/Translate/tests/phpunit/TranslateSandboxTest.php b/www/wiki/extensions/Translate/tests/phpunit/TranslateSandboxTest.php new file mode 100644 index 00000000..2389ebc7 --- /dev/null +++ b/www/wiki/extensions/Translate/tests/phpunit/TranslateSandboxTest.php @@ -0,0 +1,103 @@ +<?php +/** + * Test for the utilities for the sandbox feature of Translate. + * + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +/** + * @group Database + */ +class TranslateSandboxTest extends MediaWikiTestCase { + protected function setUp() { + parent::setUp(); + $this->setMwGlobals( [ + 'wgTranslateUseSandbox' => true, + 'wgTranslateSandboxPromotedGroup' => 'translator', + ] ); + + // Make sure the hooks are installed even if $wgTranslateUseSandbox is false. + TranslateHooks::setupTranslate(); + } + + public function testAddUser() { + $user = TranslateSandbox::addUser( 'Test user', 'test@blackhole.io', 'test password' ); + $this->assertTrue( $user->isLoggedIn(), 'User exists' ); + + $groups = array_unique( $user->getGroups() ); + + $this->assertSame( [ 'translate-sandboxed' ], $groups, 'User is in the sandboxed group' ); + } + + public function testDeleteUser() { + $user = TranslateSandbox::addUser( 'Test user2', 'test@blackhole.io', 'test password' ); + TranslateSandbox::deleteUser( $user ); + $this->assertFalse( $user->isLoggedIn(), 'User no longer exists' ); + } + + public function testDeleteUserPromoted() { + $user = TranslateSandbox::addUser( 'Test user3', 'test@blackhole.io', 'test password' ); + TranslateSandbox::promoteUser( $user ); + $this->setExpectedException( MWException::class, 'Not a sandboxed user' ); + TranslateSandbox::deleteUser( $user ); + } + + public function testGetUsers() { + $atStart = TranslateSandbox::getUsers()->count(); + + $user = TranslateSandbox::addUser( 'Test user4', 'test@blackhole.io', 'test password' ); + + $this->assertEquals( + $atStart + 1, TranslateSandbox::getUsers()->count(), + 'One sandboxed user created' + ); + + TranslateSandbox::deleteUser( $user ); + $this->assertEquals( + $atStart, TranslateSandbox::getUsers()->count(), + 'No sandboxed users after deleted' + ); + } + + public function testGetUsersPromotion() { + $atStart = TranslateSandbox::getUsers()->count(); + + $user = TranslateSandbox::addUser( 'Test user5', 'test@blackhole.io', 'test password' ); + $this->assertEquals( + $atStart + 1, + TranslateSandbox::getUsers()->count(), + 'One sandboxed user created' + ); + + TranslateSandbox::promoteUser( $user ); + $this->assertEquals( + $atStart, + TranslateSandbox::getUsers()->count(), + 'No sandboxed users after promotion' + ); + } + + public function testPromoteUser() { + $user = TranslateSandbox::addUser( 'Test user6', 'test@blackhole.io', 'test password' ); + TranslateSandbox::promoteUser( $user ); + + $this->assertContains( 'translator', $user->getGroups() ); + } + + public function testPermissions() { + $user = TranslateSandbox::addUser( 'Test user7', 'test@blackhole.io', 'test password' ); + $title = Title::makeTitle( NS_USER_TALK, $user->getName() ); + + $this->assertFalse( + $title->userCan( 'edit', $user ), + 'Sandboxed users cannot edit their own talk page' + ); + TranslateSandbox::promoteUser( $user ); + $this->assertTrue( + $title->userCan( 'edit', $user ), + 'Promoted users can edit their own talk page' + ); + } +} |