Skip to content

Commit

Permalink
php mess cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Sep 24, 2024
1 parent 462db3d commit 7efc301
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
17 changes: 12 additions & 5 deletions tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@
*/
final class external_test extends \advanced_testcase {

/** @var object That that is used for testing
* It contains an instance of the townsquare external class.
* */
private $testdata;

public function setUp(): void {
$this->resetAfterTest();
$this->testdata = new stdClass();
$this->testdata->external = new external();
}

/**
Expand All @@ -69,7 +76,7 @@ public function test_record_usersettings(): void {
$this->assertEquals(false, $record);

// Call the function to record the user settings and check, if the record is created.
$result = external::record_usersettings($usersetting->userid,
$result = $this->testdata->external->record_usersettings($usersetting->userid,
$usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);
Expand All @@ -92,7 +99,7 @@ public function test_record_usersettings(): void {
$usersetting->postletter = 0;

// Call the function to record the user settings and check, if the record is created.
$result = external::record_usersettings($usersetting->userid, $usersetting->timefilterpast,
$result = $this->testdata->external->record_usersettings($usersetting->userid, $usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);

Expand All @@ -118,22 +125,22 @@ public function test_reset_usersettings(): void {
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 1: Wrong parameters.
external::reset_usersettings(2);
$this->testdata->external->reset_usersettings(2);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 2: For some reason, many records from the same user exist.
$DB->insert_record('block_townsquare_preferences', ['userid' => 1, 'timefilterpast' => 432000,
'timefilterfuture' => 2592000, 'basicletter' => 1, 'completionletter' => 0, 'postletter' => 0, ]);
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

external::reset_usersettings(1);
$this->testdata->external->reset_usersettings(1);
$this->assertEquals(0, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 3: normal case.
$DB->insert_record('block_townsquare_preferences', ['userid' => 1, 'timefilterpast' => 432000,
'timefilterfuture' => 2592000, 'basicletter' => 0, 'completionletter' => 1, 'postletter' => 1, ]);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));
external::reset_usersettings(1);
$this->testdata->external->reset_usersettings(1);
$this->assertEquals(0, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));
}
}
30 changes: 16 additions & 14 deletions tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class provider_test extends provider_testcase {

/** @var object The data that will be used for testing.
* This Class contains:
* - the privacy provider
* - a course
* - a student and a teacher and context for both
*/
Expand All @@ -52,6 +53,7 @@ final class provider_test extends provider_testcase {
public function setUp(): void {
// Create a course and a user.
$this->testdata = new stdClass();
$this->testdata->provider = new provider();
$this->testdata->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
$this->testdata->student = $this->getDataGenerator()->create_user();
$this->testdata->studentcontext = \context_user::instance($this->testdata->student->id);
Expand All @@ -70,12 +72,12 @@ public function setUp(): void {
*/
public function test_get_contexts_for_userid(): void {
// Check that no context are found before the setting is set.
$contextlist = provider::get_contexts_for_userid($this->testdata->student->id);
$contextlist = $this->testdata->provider->get_contexts_for_userid($this->testdata->student->id);
$this->assertEquals(0, count($contextlist));

// Add a usersetting.
$this->helper_add_preference($this->testdata->student->id);
$contextlist = provider::get_contexts_for_userid($this->testdata->student->id);
$contextlist = $this->testdata->provider->get_contexts_for_userid($this->testdata->student->id);

// Check the context that should be there.
$this->assertEquals(1, count($contextlist));
Expand All @@ -89,12 +91,12 @@ public function test_get_contexts_for_userid(): void {
public function test_get_users_in_context(): void {
// Check that no users are found in the context before the setting is set.
$userlist = new userlist($this->testdata->studentcontext, 'block_townsquare');
provider::get_users_in_context($userlist);
$this->testdata->provider->get_users_in_context($userlist);
$this->assertEquals(0 , count($userlist));

// Add a usersetting.
$this->helper_add_preference($this->testdata->student->id);
provider::get_users_in_context($userlist);
$this->testdata->provider->get_users_in_context($userlist);

// Check that the provider fetches the right data.
$this->assertEquals(1, count($userlist));
Expand All @@ -108,7 +110,7 @@ public function test_get_users_in_context(): void {
*/
public function test_get_metadata(): void {
// Get the metadata and check if a collection exists.
$metadata = provider::get_metadata(new collection('block_townsquare'));
$metadata = $this->testdata->provider->get_metadata(new collection('block_townsquare'));
$collection = $metadata->get_collection();
$this->assertEquals(1, count($collection));

Expand Down Expand Up @@ -142,7 +144,7 @@ public function test_export_user_data(): void {
// Export the data.
$approvedlist = new approved_contextlist($this->testdata->student, 'block_townsquare',
[$this->testdata->studentcontext->id]);
provider::export_user_data($approvedlist);
$this->testdata->provider->export_user_data($approvedlist);
$writer = writer::with_context($this->testdata->studentcontext);
$this->assertEquals(true, $writer->has_any_data());
}
Expand All @@ -158,11 +160,11 @@ public function test_delete_data_for_all_users_in_context(): void {
$this->helper_add_preference($this->testdata->teacher->id);

// Try a system context deletion, which should have no effect.
provider::delete_data_for_all_users_in_context(\context_system::instance());
$this->testdata->provider->delete_data_for_all_users_in_context(\context_system::instance());
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences')));

// Delete all data in the first users context (studentcontext).
provider::delete_data_for_all_users_in_context(\context_user::instance($this->testdata->student->id));
$this->testdata->provider->delete_data_for_all_users_in_context(\context_user::instance($this->testdata->student->id));

// Only students data should be deleted.
$records = $DB->get_records('block_townsquare_preferences');
Expand All @@ -180,19 +182,19 @@ public function test_delete_data_for_user(): void {
$this->helper_add_preference($this->testdata->teacher->id);

// Try a system context deletion, which should have no effect.
provider::delete_data_for_all_users_in_context(\context_system::instance());
$this->testdata->provider->delete_data_for_all_users_in_context(\context_system::instance());
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences')));

// Try to delete the teacher data in a students context, which should have no effect.
$approvedlist = new approved_contextlist($this->testdata->teacher, 'block_townsquare',
[$this->testdata->studentcontext->id]);
provider::delete_data_for_user($approvedlist);
$this->testdata->provider->delete_data_for_user($approvedlist);
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences')));

// Delete the teacher date in its own context.
$approvedlist = new approved_contextlist($this->testdata->teacher, 'block_townsquare',
[$this->testdata->teachercontext->id]);
provider::delete_data_for_user($approvedlist);
$this->testdata->provider->delete_data_for_user($approvedlist);
$record = $DB->get_record('block_townsquare_preferences', ['userid' => $this->testdata->student->id]);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences')));
$this->assertEquals($this->testdata->student->id, $record->userid);
Expand All @@ -209,17 +211,17 @@ public function test_delete_data_for_users(): void {
$this->helper_add_preference($this->testdata->teacher->id);

// Try a system context deletion, which should have no effect.
provider::delete_data_for_all_users_in_context(\context_system::instance());
$this->testdata->provider->delete_data_for_all_users_in_context(\context_system::instance());
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences')));

// Try to delete user data in another users context, which should have no effect..
$approvedlist = new approved_userlist($this->testdata->studentcontext, 'block_townsquare', [$this->testdata->teacher->id]);
provider::delete_data_for_users($approvedlist);
$this->testdata->provider->delete_data_for_users($approvedlist);
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences')));

// Delete user data in the right context.
$approvedlist = new approved_userlist($this->testdata->studentcontext, 'block_townsquare', [$this->testdata->student->id]);
provider::delete_data_for_users($approvedlist);
$this->testdata->provider->delete_data_for_users($approvedlist);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences')));
$record = $DB->get_record('block_townsquare_preferences', ['userid' => $this->testdata->teacher->id]);
$this->assertEquals($this->testdata->teacher->id, $record->userid);
Expand Down

0 comments on commit 7efc301

Please sign in to comment.