Skip to content

Commit

Permalink
Refactors moderation reminder helper
Browse files Browse the repository at this point in the history
Issue: documentacao-e-tarefas/scielo#703

Signed-off-by: Jhon <[email protected]>
  • Loading branch information
JhonathanLepidus committed Oct 7, 2024
1 parent d7b810d commit c745a88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 123 deletions.
44 changes: 0 additions & 44 deletions classes/ModerationReminderHelper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,6 @@ public function getResponsiblesUserGroup(int $contextId)
return $responsiblesUserGroup;
}

public function getResponsibleAssignments($responsiblesUserGroup, $contextId): array
{
if (!$responsiblesUserGroup) {
return [];
}

$stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
$responsiblesAssignments = $stageAssignmentDao->getByUserGroupId($responsiblesUserGroup->getId(), $contextId);

return $responsiblesAssignments->toArray();
}

public function filterAssignmentsOfSubmissionsOnPreModeration(array $assignments): array
{
$preModerationAssignments = [];

foreach ($assignments as $assignment) {
$submissionId = $assignment->getData('submissionId');
$submissionModerationStage = $this->moderationStageDao->getSubmissionModerationStage($submissionId);

if ($submissionModerationStage === SCIELO_MODERATION_STAGE_CONTENT) {
$preModerationAssignments[] = $assignment;
}
}

return $preModerationAssignments;
}

public function getUsersFromAssignments(array $assignments): array
{
$users = [];
$userDao = DAORegistry::getDAO('UserDAO');

foreach ($assignments as $assignment) {
$user = $userDao->getById($assignment->getUserId());

if ($user and !isset($users[$user->getId()])) {
$users[$user->getId()] = $user;
}
}

return $users;
}

public function mapUsersAndSubmissions($users, $assignments)
{
$usersMap = [];
Expand Down
117 changes: 38 additions & 79 deletions tests/ModerationReminderHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,71 @@

use PHPUnit\Framework\TestCase;

import('lib.pkp.classes.user.User');
import('lib.pkp.classes.user.UserDAO');
import('lib.pkp.classes.stageAssignment.StageAssignment');
import('plugins.generic.scieloModerationStages.classes.ModerationStage');
import('plugins.generic.scieloModerationStages.classes.ModerationStageDAO');
import('lib.pkp.classes.db.DAOResultFactory');
import('lib.pkp.classes.security.UserGroup');
import('plugins.generic.scieloModerationStages.classes.ModerationReminderHelper');

class ModerationReminderHelperTest extends TestCase
{
private $moderationReminderHelper;
private $moderatorUsers;
private $assignments;
private $userGroups;
private $locale = 'en_US';

public function setUp(): void
{
$this->moderationReminderHelper = new ModerationReminderHelper();
$this->moderatorUsers = $this->createTestModeratorUsers();
$this->assignments = $this->createTestAssignments();
$this->userGroups = $this->createTestUserGroups();
}

protected function getMockedDAOs()
{
return ['UserDAO'];
return ['UserGroupDAO', 'SubmissionDAO'];
}

private function createTestModeratorUsers(): array
private function createTestUserGroups(): array
{
$firstModerator = new User();
$firstModerator->setData('id', 312);
$firstModerator->setGivenName('Edgar', $this->locale);
$firstModerator->setFamilyName('Linton', $this->locale);
$firstUserGroup = new UserGroup();
$firstUserGroup->setData('id', 12);
$firstUserGroup->setData('abbrev', 'SciELO', $this->locale);

$secondModerator = new User();
$secondModerator->setData('id', 313);
$secondModerator->setGivenName('Catherine', $this->locale);
$secondModerator->setFamilyName('Earnshaw', $this->locale);
$secondUserGroup = new UserGroup();
$secondUserGroup->setData('id', 13);
$secondUserGroup->setData('abbrev', 'RESP', $this->locale);

return [$firstModerator, $secondModerator];
return [$firstUserGroup, $secondUserGroup];
}

private function createTestAssignments(): array
private function registerUserGroupDaoMock()
{
$firstAssignment = new StageAssignment();
$firstAssignment->setData('submissionId', 256);
$firstAssignment->setData('stageId', WORKFLOW_STAGE_ID_SUBMISSION);
$firstAssignment->setData('userId', $this->moderatorUsers[0]->getId());

$secondAssignment = new StageAssignment();
$secondAssignment->setData('submissionId', 257);
$secondAssignment->setData('stageId', WORKFLOW_STAGE_ID_SUBMISSION);
$secondAssignment->setData('userId', $this->moderatorUsers[0]->getId());

$thirdAssignment = new StageAssignment();
$thirdAssignment->setData('submissionId', 258);
$thirdAssignment->setData('stageId', WORKFLOW_STAGE_ID_SUBMISSION);
$thirdAssignment->setData('userId', $this->moderatorUsers[1]->getId());

return [$firstAssignment, $secondAssignment, $thirdAssignment];
}

private function createModerationStageDaoMock()
{
$mockedDAO = $this->createMock(ModerationStageDAO::class);
$mockedDAO->method('getSubmissionModerationStage')->willReturnMap([
[256, SCIELO_MODERATION_STAGE_CONTENT],
[257, SCIELO_MODERATION_STAGE_FORMAT],
[258, SCIELO_MODERATION_STAGE_CONTENT]
]);

return $mockedDAO;
}

private function registerUserDaoMock()
{
$mockedUserDao = $this->getMockBuilder(UserDAO::class)
->setMethods(['getById'])
$mockDaoResultFactory = $this->getMockBuilder(UserDAO::class)
->setMethods(['toArray'])
->getMock();
$mockedUserDao->expects($this->any())
->method('getById')
->will($this->onConsecutiveCalls($this->moderatorUsers[0], $this->moderatorUsers[1]));

DAORegistry::registerDAO('UserDAO', $mockedUserDao);
}

public function testFilterAssignmentsOfSubmissionsOnPreModeration(): void
{
$mockedModerationStageDao = $this->createModerationStageDaoMock();
$this->moderationReminderHelper->setModerationStageDao($mockedModerationStageDao);

$expectedFilteredAssignments = [$this->assignments[0], $this->assignments[2]];
$filteredAssignments = $this->moderationReminderHelper->filterAssignmentsOfSubmissionsOnPreModeration($this->assignments);
$mockDaoResultFactory->expects($this->any())
->method('toArray')
->will($this->returnValue($this->userGroups));

$this->assertEquals($expectedFilteredAssignments, $filteredAssignments);
$mockedUserGroupDao = $this->getMockBuilder(UserDAO::class)
->setMethods(['getByContextId', 'getSetting'])
->getMock();
$mockedUserGroupDao->expects($this->any())
->method('getByContextId')
->will($this->returnValue($mockDaoResultFactory));
$mockedUserGroupDao->expects($this->any())
->method('getSetting')
->will($this->onConsecutiveCalls(
$this->userGroups[0]->getData('abbrev', $this->locale),
$this->userGroups[1]->getData('abbrev', $this->locale)
));

DAORegistry::registerDAO('UserGroupDAO', $mockedUserGroupDao);
}

public function testGetUsersFromAssignments(): void
public function testGetResponsiblesUserGroup(): void
{
$this->registerUserDaoMock();

$expectedAssignedUsers = [
$this->moderatorUsers[1]->getId() => $this->moderatorUsers[1],
$this->moderatorUsers[0]->getId() => $this->moderatorUsers[0],
];
$usersFromAssignments = $this->moderationReminderHelper->getUsersFromAssignments($this->assignments);
$this->registerUserGroupDaoMock();
$contextId = 1;
$responsiblesUserGroup = $this->moderationReminderHelper->getResponsiblesUserGroup($contextId);

$this->assertEquals($expectedAssignedUsers, $usersFromAssignments);
$this->assertEquals($this->userGroups[1]->getId(), $responsiblesUserGroup->getId());
}
}

0 comments on commit c745a88

Please sign in to comment.