Skip to content

Commit

Permalink
MDL-81281 phpunit: assertObjectNotHasAttribute is deprecated
Browse files Browse the repository at this point in the history
To be integrated as part of MDL-81266

When running PHPUnit 9.6 we get the following deprecation warnings:
"assertObjectNotHasAttribute() is deprecated and will be removed in PHPUnit
10. Refactor your test to use assertObjectNotHasProperty() instead."

So we replace all instances of assertObjectNotHasAttribute with
assertObjectNotHasProperty.

PHPUnit justifies the change with:
> PHPUnit currently refers to "fields" (see above) as "attributes". This
> is (or will become) confusing considering the introduction of
> attributes in PHP 8 and their support in PHPUnit.  PHPUnit will be
> changed to use the term "property" instead of "attribute" where "field"
> is meant.

Signed-off-by: Daniel Ziegenberg <[email protected]>
  • Loading branch information
ziegenberg authored and stronk7 committed Mar 25, 2024
1 parent 51bc492 commit 87267da
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 86 deletions.
10 changes: 5 additions & 5 deletions auth/tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public function test_export_user_preferences() {
$prefs = writer::with_context($sysctx)->get_user_preferences('core_auth');
$this->assertEquals(transform::yesno(false), $prefs->auth_forcepasswordchange->value);
$this->assertEquals(transform::yesno(false), $prefs->create_password->value);
$this->assertObjectNotHasAttribute('login_failed_count', $prefs);
$this->assertObjectNotHasAttribute('login_failed_count_since_success', $prefs);
$this->assertObjectNotHasAttribute('login_failed_last', $prefs);
$this->assertObjectNotHasAttribute('login_lockout', $prefs);
$this->assertObjectNotHasProperty('login_failed_count', $prefs);
$this->assertObjectNotHasProperty('login_failed_count_since_success', $prefs);
$this->assertObjectNotHasProperty('login_failed_last', $prefs);
$this->assertObjectNotHasProperty('login_lockout', $prefs);
$this->assertEquals(transform::yesno(true), $prefs->login_lockout_ignored->value);
$this->assertObjectNotHasAttribute('login_lockout_secret', $prefs);
$this->assertObjectNotHasProperty('login_lockout_secret', $prefs);
}
}
4 changes: 2 additions & 2 deletions blocks/tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,12 @@ public function test_export_data_for_user() {
$this->assertEquals($yes, $prefs->block_is_hidden->value);

$prefs = writer::with_context($bprivatefiles->context)->get_user_context_preferences('core_block');
$this->assertObjectNotHasAttribute('block_is_docked', $prefs);
$this->assertObjectNotHasProperty('block_is_docked', $prefs);
$this->assertEquals($no, $prefs->block_is_hidden->value);

$prefs = writer::with_context($bmyprofile->context)->get_user_context_preferences('core_block');
$this->assertEquals($yes, $prefs->block_is_docked->value);
$this->assertObjectNotHasAttribute('block_is_hidden', $prefs);
$this->assertObjectNotHasProperty('block_is_hidden', $prefs);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions cohort/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ public function test_get_functions_return_custom_fields() {

// Test custom fields are not returned if not needed.
$result = cohort_get_cohort($cohort1->id, $coursectx);
$this->assertObjectNotHasAttribute('customfields', $result);
$this->assertObjectNotHasProperty('customfields', $result);

// Test cohort_get_cohorts.
$result = cohort_get_cohorts(\context_system::instance()->id, 0, 25, '', true);
Expand All @@ -745,7 +745,7 @@ public function test_get_functions_return_custom_fields() {
$this->assertEquals(2, $result['totalcohorts']);
$this->assertEquals(2, $result['allcohorts']);
foreach ($result['cohorts'] as $cohort) {
$this->assertObjectNotHasAttribute('customfields', $cohort);
$this->assertObjectNotHasProperty('customfields', $cohort);
}

// Test test_cohort_get_all_cohorts.
Expand All @@ -771,7 +771,7 @@ public function test_get_functions_return_custom_fields() {
$this->assertEquals(2, $result['totalcohorts']);
$this->assertEquals(2, $result['allcohorts']);
foreach ($result['cohorts'] as $cohort) {
$this->assertObjectNotHasAttribute('customfields', $cohort);
$this->assertObjectNotHasProperty('customfields', $cohort);
}

// Test cohort_get_available_cohorts.
Expand All @@ -795,7 +795,7 @@ public function test_get_functions_return_custom_fields() {
$result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, '');
$this->assertCount(2, $result);
foreach ($result as $cohort) {
$this->assertObjectNotHasAttribute('customfields', $cohort);
$this->assertObjectNotHasProperty('customfields', $cohort);
}

// Test cohort_get_user_cohorts.
Expand All @@ -822,7 +822,7 @@ public function test_get_functions_return_custom_fields() {
$result = cohort_get_user_cohorts($user->id);
$this->assertCount(2, $result);
foreach ($result as $cohort) {
$this->assertObjectNotHasAttribute('customfields', $cohort);
$this->assertObjectNotHasProperty('customfields', $cohort);
}
}

Expand Down
2 changes: 1 addition & 1 deletion course/format/tests/external/update_course_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function test_execute_course_state(
$update = $this->find_update($results, $expected['action'], 'cm', $activity->cmid);
$this->assertNotEmpty($update);
if ($expected['visible'] === null) {
$this->assertObjectNotHasAttribute('visible', $update->fields);
$this->assertObjectNotHasProperty('visible', $update->fields);
} else {
$this->assertEquals($expected['visible'], $update->fields->visible);
}
Expand Down
10 changes: 5 additions & 5 deletions course/format/tests/output/activitybadge_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,31 @@ private function check_activitybadge(
?array $extra = null
): void {
if (is_null($content)) {
$this->assertObjectNotHasAttribute('badgecontent', $result);
$this->assertObjectNotHasProperty('badgecontent', $result);
} else {
$this->assertEquals($content, $result->badgecontent);
}

if (is_null($style)) {
$this->assertObjectNotHasAttribute('badgestyle', $result);
$this->assertObjectNotHasProperty('badgestyle', $result);
} else {
$this->assertEquals($style, $result->badgestyle);
}

if (is_null($url)) {
$this->assertObjectNotHasAttribute('badgeurl', $result);
$this->assertObjectNotHasProperty('badgeurl', $result);
} else {
$this->assertEquals($url, $result->badgeurl);
}

if (is_null($elementid)) {
$this->assertObjectNotHasAttribute('badgeelementid', $result);
$this->assertObjectNotHasProperty('badgeelementid', $result);
} else {
$this->assertEquals($elementid, $result->badgeelementid);
}

if (is_null($extra)) {
$this->assertObjectNotHasAttribute('badgeextraattributes', $result);
$this->assertObjectNotHasProperty('badgeextraattributes', $result);
} else {
$this->assertEquals($extra, $result->badgeextraattributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public function test_user_launches_tool_force_embedding_custom_param() {

// Instructors aren't subject to forceembed.
$launchservice->user_launches_tool($instructoruser, $mockinstructorlaunch);
$this->assertObjectNotHasAttribute('forcepagelayout', $SESSION);
$this->assertObjectNotHasProperty('forcepagelayout', $SESSION);

// Learners are.
$launchservice->user_launches_tool($learneruser, $mocklearnerlaunch);
Expand Down
6 changes: 3 additions & 3 deletions enrol/tests/course_enrolment_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function test_get_users_fields() {
$this->assertEquals('Smart suit', $user->imagealt);

// But not some random other field like city.
$this->assertObjectNotHasAttribute('city', $user);
$this->assertObjectNotHasProperty('city', $user);
}

/**
Expand Down Expand Up @@ -330,7 +330,7 @@ public function test_get_other_users_fields() {
$this->assertEquals('Smart suit', $user->imagealt);

// But not some random other field like city.
$this->assertObjectNotHasAttribute('city', $user);
$this->assertObjectNotHasProperty('city', $user);
}

/**
Expand Down Expand Up @@ -367,7 +367,7 @@ public function test_get_potential_users_fields() {
$this->assertEquals('Smart suit', $user->imagealt);

// But not some random other field like city.
$this->assertObjectNotHasAttribute('city', $user);
$this->assertObjectNotHasProperty('city', $user);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions enrol/tests/enrollib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,14 +954,14 @@ public function test_enrol_get_my_courses_all_accessible() {
// Check fields parameter still works. Fields default (certain base fields).
$this->assertObjectHasAttribute('id', $courses[$course3->id]);
$this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
$this->assertObjectNotHasAttribute('summary', $courses[$course3->id]);
$this->assertObjectNotHasProperty('summary', $courses[$course3->id]);

// Specified fields (one, string).
$courses = enrol_get_my_courses('summary', 'id', 0, [], true);
$this->assertObjectHasAttribute('id', $courses[$course3->id]);
$this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
$this->assertObjectHasAttribute('summary', $courses[$course3->id]);
$this->assertObjectNotHasAttribute('summaryformat', $courses[$course3->id]);
$this->assertObjectNotHasProperty('summaryformat', $courses[$course3->id]);

// Specified fields (two, string).
$courses = enrol_get_my_courses('summary, summaryformat', 'id', 0, [], true);
Expand Down
2 changes: 1 addition & 1 deletion lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function test_set_user() {
// Ensure session is reset after setUser, as it may contain extra info.
$SESSION->sometestvalue = true;
$this->setUser($user);
$this->assertObjectNotHasAttribute('sometestvalue', $SESSION);
$this->assertObjectNotHasProperty('sometestvalue', $SESSION);
}

public function test_set_admin_user() {
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/cron_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function test_setup_user(): void {
$this->assertSame($user1->id, $USER->id);
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
$this->assertNotSame($adminsession, $SESSION);
$this->assertObjectNotHasAttribute('test1', $SESSION);
$this->assertObjectNotHasProperty('test1', $SESSION);
$this->assertEmpty((array)$SESSION);
$usersession1 = $SESSION;
$SESSION->test2 = true;
Expand Down
26 changes: 13 additions & 13 deletions lib/tests/datalib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function test_get_coursemodule_from_id() {
$this->assertSame('folder', $cm->modname);
$this->assertSame($folder1a->id, $cm->instance);
$this->assertSame($folder1a->course, $cm->course);
$this->assertObjectNotHasAttribute('sectionnum', $cm);
$this->assertObjectNotHasProperty('sectionnum', $cm);

$this->assertEquals($cm, get_coursemodule_from_id('', $folder1a->cmid));
$this->assertEquals($cm, get_coursemodule_from_id('folder', $folder1a->cmid, $course1->id));
Expand Down Expand Up @@ -483,7 +483,7 @@ public function test_get_coursemodule_from_instance() {
$this->assertSame('folder', $cm->modname);
$this->assertSame($folder1a->id, $cm->instance);
$this->assertSame($folder1a->course, $cm->course);
$this->assertObjectNotHasAttribute('sectionnum', $cm);
$this->assertObjectNotHasProperty('sectionnum', $cm);

$this->assertEquals($cm, get_coursemodule_from_instance('folder', $folder1a->id, $course1->id));
$this->assertEquals($cm, get_coursemodule_from_instance('folder', $folder1a->id, 0));
Expand Down Expand Up @@ -553,17 +553,17 @@ public function test_get_coursemodules_in_course() {
$this->assertSame('folder', $cm->modname);
$this->assertSame($folder1a->id, $cm->instance);
$this->assertSame($folder1a->course, $cm->course);
$this->assertObjectNotHasAttribute('sectionnum', $cm);
$this->assertObjectNotHasAttribute('revision', $cm);
$this->assertObjectNotHasAttribute('display', $cm);
$this->assertObjectNotHasProperty('sectionnum', $cm);
$this->assertObjectNotHasProperty('revision', $cm);
$this->assertObjectNotHasProperty('display', $cm);

$cm = $modules[$folder1b->cmid];
$this->assertSame('folder', $cm->modname);
$this->assertSame($folder1b->id, $cm->instance);
$this->assertSame($folder1b->course, $cm->course);
$this->assertObjectNotHasAttribute('sectionnum', $cm);
$this->assertObjectNotHasAttribute('revision', $cm);
$this->assertObjectNotHasAttribute('display', $cm);
$this->assertObjectNotHasProperty('sectionnum', $cm);
$this->assertObjectNotHasProperty('revision', $cm);
$this->assertObjectNotHasProperty('display', $cm);

$modules = get_coursemodules_in_course('folder', $course1->id, 'revision, display');
$this->assertCount(2, $modules);
Expand All @@ -572,7 +572,7 @@ public function test_get_coursemodules_in_course() {
$this->assertSame('folder', $cm->modname);
$this->assertSame($folder1a->id, $cm->instance);
$this->assertSame($folder1a->course, $cm->course);
$this->assertObjectNotHasAttribute('sectionnum', $cm);
$this->assertObjectNotHasProperty('sectionnum', $cm);
$this->assertObjectHasAttribute('revision', $cm);
$this->assertObjectHasAttribute('display', $cm);

Expand Down Expand Up @@ -845,8 +845,8 @@ public function test_get_users_listing(): void {
$this->assertObjectHasAttribute('firstnamephonetic', $results[$userids[0]]);

// Should not have the custom field or department because no context specified.
$this->assertObjectNotHasAttribute('department', $results[$userids[0]]);
$this->assertObjectNotHasAttribute('profile_field_specialid', $results[$userids[0]]);
$this->assertObjectNotHasProperty('department', $results[$userids[0]]);
$this->assertObjectNotHasProperty('profile_field_specialid', $results[$userids[0]]);

// Check sorting.
$results = get_users_listing('username', 'DESC');
Expand All @@ -867,8 +867,8 @@ public function test_get_users_listing(): void {
// specify a context AND have permissions.
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', '', null,
\context_system::instance());
$this->assertObjectNotHasAttribute('department', $results[$userids[0]]);
$this->assertObjectNotHasAttribute('profile_field_specialid', $results[$userids[0]]);
$this->assertObjectNotHasProperty('department', $results[$userids[0]]);
$this->assertObjectNotHasProperty('profile_field_specialid', $results[$userids[0]]);
$this->setAdminUser();
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', '', null,
\context_system::instance());
Expand Down
12 changes: 6 additions & 6 deletions lib/tests/filetypes_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,36 +214,36 @@ public function test_cleanup() {
$this->resetAfterTest();

// The custom filetypes setting is empty to start with.
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);

// Add a custom filetype, then delete it.
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::delete_type('frog');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);

// Change a standard filetype, then change it back.
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);

// Delete a standard filetype, then add it back (the same).
core_filetypes::delete_type('asm');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::add_type('asm', 'text/plain', 'sourcecode');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);

// Revert a changed type.
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::revert_type_to_default('asm');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);

// Revert a deleted type.
core_filetypes::delete_type('asm');
$this->assertObjectHasAttribute('customfiletypes', $CFG);
core_filetypes::revert_type_to_default('asm');
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
$this->assertObjectNotHasProperty('customfiletypes', $CFG);
}
}
4 changes: 2 additions & 2 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3159,8 +3159,8 @@ public function test_complete_user_login() {
$this->assertTimeCurrent($USER->currentlogin);
$this->assertSame(sesskey(), $USER->sesskey);
$this->assertTimeCurrent($USER->preference['_lastloaded']);
$this->assertObjectNotHasAttribute('password', $USER);
$this->assertObjectNotHasAttribute('description', $USER);
$this->assertObjectNotHasProperty('password', $USER);
$this->assertObjectNotHasProperty('description', $USER);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/tests/session_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public function test_set_user() {
\core\session\manager::set_user($user);

$this->assertEquals($user->id, $USER->id);
$this->assertObjectNotHasAttribute('description', $user);
$this->assertObjectNotHasAttribute('password', $user);
$this->assertObjectNotHasProperty('description', $user);
$this->assertObjectNotHasProperty('password', $user);
$this->assertObjectHasAttribute('sesskey', $user);
$this->assertSame($user, $GLOBALS['USER']);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
Expand All @@ -124,8 +124,8 @@ public function test_login_user() {
@\core\session\manager::login_user($user); // Ignore header error messages.
$this->assertEquals($user->id, $USER->id);

$this->assertObjectNotHasAttribute('description', $user);
$this->assertObjectNotHasAttribute('password', $user);
$this->assertObjectNotHasProperty('description', $user);
$this->assertObjectNotHasProperty('password', $user);
$this->assertSame($user, $GLOBALS['USER']);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
$this->assertSame($GLOBALS['USER'], $USER);
Expand Down Expand Up @@ -558,7 +558,7 @@ public function test_loginas() {
$_SESSION['extra'] = true;

// Try admin loginas this user in system context.
$this->assertObjectNotHasAttribute('realuser', $USER);
$this->assertObjectNotHasProperty('realuser', $USER);
\core\session\manager::loginas($user->id, \context_system::instance());

$this->assertSame($user->id, $USER->id);
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/sessionlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function test_cron_setup_user() {
$this->assertSame($user1->id, $USER->id);
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
$this->assertNotSame($adminsession, $SESSION);
$this->assertObjectNotHasAttribute('test1', $SESSION);
$this->assertObjectNotHasProperty('test1', $SESSION);
$this->assertEmpty((array)$SESSION);
$usersession1 = $SESSION;
$SESSION->test2 = true;
Expand Down Expand Up @@ -261,7 +261,7 @@ public function test_sesskey() {
$user = $this->getDataGenerator()->create_user();

\core\session\manager::init_empty_session();
$this->assertObjectNotHasAttribute('sesskey', $USER);
$this->assertObjectNotHasProperty('sesskey', $USER);

$sesskey = sesskey();
$this->assertNotEmpty($sesskey);
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/upgradelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ public function test_moodle_upgrade_check_outageless() {
public function test_moodle_start_upgrade_outageless() {
global $CFG;
$this->resetAfterTest();
$this->assertObjectNotHasAttribute('upgraderunning', $CFG);
$this->assertObjectNotHasProperty('upgraderunning', $CFG);

// Confirm that starting normally sets the upgraderunning flag.
upgrade_started();
Expand All @@ -1592,7 +1592,7 @@ public function test_moodle_start_upgrade_outageless() {
public function test_moodle_set_upgrade_timeout_outageless() {
global $CFG;
$this->resetAfterTest();
$this->assertObjectNotHasAttribute('upgraderunning', $CFG);
$this->assertObjectNotHasProperty('upgraderunning', $CFG);

// Confirm running normally sets the timeout.
upgrade_set_timeout(120);
Expand Down
Loading

0 comments on commit 87267da

Please sign in to comment.