diff --git a/classes/ModerationReminderHelper.inc.php b/classes/ModerationReminderHelper.inc.php index ac15762..5948ddc 100644 --- a/classes/ModerationReminderHelper.inc.php +++ b/classes/ModerationReminderHelper.inc.php @@ -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 = []; diff --git a/tests/ModerationReminderHelperTest.php b/tests/ModerationReminderHelperTest.php index 2ab7359..2c729d8 100644 --- a/tests/ModerationReminderHelperTest.php +++ b/tests/ModerationReminderHelperTest.php @@ -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()); } }