Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple categories users fix #94

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/EntryPoints/Specials/SpecialManageApprovers.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public function execute( $subPage ): void {
$this->checkPermissions();
$this->checkReadOnly();

$approversWithCategories = new GetApproversWithCategories( $this->approverRepository );
$approversCategories = $approversWithCategories->getApproversWithCategories();

$request = $this->getRequest();
if ( $request->wasPosted() ) {
$this->handlePostRequest( $request, $approversCategories );
$this->handlePostRequest( $request );
$this->getOutput()->redirect( $this->getPageTitle()->getLocalURL() );
return;
}

$approversWithCategories = new GetApproversWithCategories( $this->approverRepository );
$approversCategories = $approversWithCategories->getApproversWithCategories();

$this->renderHtml( $approversCategories );

$this->getOutput()->addModuleStyles( 'ext.pageApprovals.manageApprovers.styles' );
Expand All @@ -50,10 +50,7 @@ private function isAdmin(): bool {
return in_array( 'sysop', $userGroups );
}

/**
* @param array<Approver> $approversCategories
*/
private function handlePostRequest( WebRequest $request, array $approversCategories ): void {
private function handlePostRequest( WebRequest $request ): void {
$action = $request->getText( 'action' );
$username = $request->getText( 'username' );
$category = $request->getText( 'category' );
Expand All @@ -65,17 +62,12 @@ private function handlePostRequest( WebRequest $request, array $approversCategor
return;
}

$userWithCategories = array_filter( $approversCategories, fn( $approver ) => $approver->username === $username
);
$currentCategories = $userWithCategories[0]->categories ?? [];

$this->processCategoryAction( $action, $category, $userId, $currentCategories );
$this->processCategoryAction( $action, $category, $userId );
}

/**
* @param string[] $currentCategories
*/
private function processCategoryAction( string $action, string $category, int $userId, array $currentCategories ): void {
private function processCategoryAction( string $action, string $category, int $userId ): void {
$currentCategories = $this->approverRepository->getApproverCategories( $userId );

switch ( $action ) {
case 'add':
$currentCategories[] = $category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FauxRequest;
use PermissionsError;
use ProfessionalWiki\PageApprovals\Application\ApproverRepository;
use ProfessionalWiki\PageApprovals\EntryPoints\Specials\SpecialManageApprovers;
use ProfessionalWiki\PageApprovals\PageApprovals;
use SpecialPageTestBase;
Expand All @@ -13,11 +14,18 @@
* @group Database
* @covers \ProfessionalWiki\PageApprovals\EntryPoints\Specials\SpecialManageApprovers
*/
class SpecialApproverCategoriesTest extends SpecialPageTestBase {
class SpecialManageApproversTest extends SpecialPageTestBase {

private ApproverRepository $approverRepository;

protected function setUp(): void {
parent::setUp();
$this->approverRepository = PageApprovals::getInstance()->getApproverRepository();
}

protected function newSpecialPage(): SpecialManageApprovers {
return new SpecialManageApprovers(
PageApprovals::getInstance()->getApproverRepository(),
$this->approverRepository,
$this->getServiceContainer()->getUserGroupManager(),
$this->getServiceContainer()->getUserFactory()
);
Expand Down Expand Up @@ -103,4 +111,46 @@ public function testAddAndDeleteCategoryAction(): void {
$this->assertStringNotContainsString( 'TestCategory', $this->viewPage(), 'Category should be deleted' );
}

public function testCanAddAnotherCategoryWhenMultipleUsersHaveMultipleCategories(): void {
$user1 = self::getTestUser( [ 'Group1' ] )->getUser();
$user2 = self::getTestUser( [ 'Group2' ] )->getUser();
$user3 = self::getTestUser( [ 'Group3' ] )->getUser();

$this->approverRepository->setApproverCategories(
$user1->getId(),
[ 'TestCategory1', 'TestCategory2' ]
);
$this->approverRepository->setApproverCategories(
$user2->getId(),
[ 'TestCategory2', 'TestCategory3' ]
);
$this->approverRepository->setApproverCategories(
$user3->getId(),
[ 'TestCategory3' ]
);

$this->post(
request: [
'action' => 'add',
'username' => $user3->getName(),
'category' => 'TestCategory4'
]
);

$this->assertSame(
[ 'TestCategory1', 'TestCategory2' ],
$this->approverRepository->getApproverCategories( $user1->getId() )
);

$this->assertSame(
[ 'TestCategory2', 'TestCategory3' ],
$this->approverRepository->getApproverCategories( $user2->getId() )
);

$this->assertSame(
[ 'TestCategory3', 'TestCategory4' ],
$this->approverRepository->getApproverCategories( $user3->getId() )
);
}

}