Skip to content

Commit

Permalink
MDL-82119 core_exemptions: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Fragonite committed Aug 29, 2024
1 parent 0fb1310 commit 1eedbb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class component_exemption_service {
* @param int|null $reasonformat The format of the reason for the exemption.
* @return exemption The exemption, once created.
*/
public function create(string $itemtype, int $itemid, int $contextid, string $reason = null, int $reasonformat = null): exemption {
public function create(string $itemtype, int $itemid, int $contextid,
?string $reason = null, ?int $reasonformat = null): exemption {

$exemption = new exemption($this->component, $itemtype, $itemid, $contextid, $reason, $reasonformat);
return $this->repo->add($exemption);
}
Expand Down
53 changes: 22 additions & 31 deletions exemptions/tests/component_exemption_service_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function get_mock_repository(array $mockstore) {
->getMock();
$mockrepo->expects($this->any())
->method('add')
->will($this->returnCallback(function(exemption $exemption) use (&$mockstore) {
->will($this->returnCallback(function (exemption $exemption) use (&$mockstore) {
// Mock implementation of repository->add(), where an array is used instead of the DB.
// Duplicates are confirmed via the unique key, and exceptions thrown just like a real repo.
$key = $exemption->component . $exemption->itemtype . $exemption->itemid
Expand All @@ -84,11 +84,10 @@ protected function get_mock_repository(array $mockstore) {
$exemption->id = $index;
$mockstore[$index] = $exemption;
return $mockstore[$index];
})
);
}));
$mockrepo->expects($this->any())
->method('update')
->will($this->returnCallback(function(exemption $exemption) use (&$mockstore) {
->will($this->returnCallback(function (exemption $exemption) use (&$mockstore) {
$key = $exemption->component . $exemption->itemtype . $exemption->itemid . $exemption->contextid;
foreach ($mockstore as $index => $item) {
if ($item->uniquekey == $key) {
Expand All @@ -97,8 +96,7 @@ protected function get_mock_repository(array $mockstore) {
}
}
throw new \moodle_exception('exemption not found');
})
);
}));
$mockrepo->expects($this->any())
->method('find_by')
->will($this->returnCallback(function (array $criteria, int $limitfrom = 0, int $limitnum = 0) use (&$mockstore) {
Expand Down Expand Up @@ -143,75 +141,68 @@ protected function get_mock_repository(array $mockstore) {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
$crit = ['component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid];
foreach ($mockstore as $fakerow) {
$fakerowarr = (array)$fakerow;
$fakerowarr = (array) $fakerow;
if (array_diff_assoc($crit, $fakerowarr) == []) {
return $fakerow;
}
}
throw new \dml_missing_record_exception("Item not found");
})
);
}));
$mockrepo->expects($this->any())
->method('find')
->will($this->returnCallback(function(int $id) use (&$mockstore) {
->will($this->returnCallback(function (int $id) use (&$mockstore) {
return $mockstore[$id];
})
);
}));
$mockrepo->expects($this->any())
->method('exists')
->will($this->returnCallback(function(int $id) use (&$mockstore) {
->will($this->returnCallback(function (int $id) use (&$mockstore) {
return array_key_exists($id, $mockstore);
})
);
}));
$mockrepo->expects($this->any())
->method('count_by')
->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
->will($this->returnCallback(function (array $criteria) use (&$mockstore) {
$count = 0;
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
$mockrowarr = (array) $mockrow;
if (array_diff_assoc($criteria, $mockrowarr) == []) {
$count++;
}
}
return $count;
})
);
}));
$mockrepo->expects($this->any())
->method('delete')
->will($this->returnCallback(function(int $id) use (&$mockstore) {
->will($this->returnCallback(function (int $id) use (&$mockstore) {
foreach ($mockstore as $mockrow) {
if ($mockrow->id == $id) {
unset($mockstore[$id]);
}
}
})
);
}));
$mockrepo->expects($this->any())
->method('delete_by')
->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
->will($this->returnCallback(function (array $criteria) use (&$mockstore) {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
$mockrowarr = (array) $mockrow;
if (array_diff_assoc($criteria, $mockrowarr) == []) {
unset($mockstore[$index]);
}
}
})
);
}));
$mockrepo->expects($this->any())
->method('exists_by')
->will($this->returnCallback(function(array $criteria) use (&$mockstore) {
->will($this->returnCallback(function (array $criteria) use (&$mockstore) {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
$mockrowarr = (array) $mockrow;
if (array_diff_assoc($criteria, $mockrowarr) == []) {
return true;
}
}
return false;
})
);
}));
return $mockrepo;
}

Expand Down Expand Up @@ -290,7 +281,7 @@ public function test_findby(): void {

$found = $service->find_by(['contextid' => [$course1ctx->id, $course2ctx->id]]);
$this->assertCount(2, $found);

$found = $service->find_by(['itemtype' => 'module']);
$this->assertCount(0, $found);
}
Expand Down
2 changes: 1 addition & 1 deletion exemptions/tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function test_delete_exemptions_for_user(): void {
$this->setUser($user1);
$service->create('user', $user1->id, $course1ctx->id, $reason, $format);
$service->create('user', $user1->id, $course2ctx->id, $reason, $format);

$this->setUser($user2);
$service->create('user', $user2->id, $course2ctx->id, $reason, $format);

Expand Down

0 comments on commit 1eedbb7

Please sign in to comment.