Skip to content

Commit

Permalink
MDL-81281 phpunit: assertObjectHasAttribute 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:
"assertObjectHasAttribute() is deprecated and will be removed in PHPUnit
10. Refactor your test to use assertObjectHasProperty() instead."

So we replace all instances of assertObjectHasAttribute with
assertObjectHasProperty.

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 87267da commit 08027e4
Show file tree
Hide file tree
Showing 40 changed files with 226 additions and 226 deletions.
2 changes: 1 addition & 1 deletion admin/tool/policy/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_policy_document_life_cycle() {

// Prepare the form data for adding a new policy document.
$formdata = api::form_policydoc_data(new policy_version(0));
$this->assertObjectHasAttribute('name', $formdata);
$this->assertObjectHasProperty('name', $formdata);
$this->assertArrayHasKey('text', $formdata->summary_editor);
$this->assertArrayHasKey('format', $formdata->content_editor);

Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploaduser/tests/cli_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function test_upload_with_profile_fields() {
// Created users have data in the profile fields.
$user1 = \core_user::get_user_by_username('reznort');
$profilefields1 = profile_user_record($user1->id);
$this->assertObjectHasAttribute('superfield', $profilefields1);
$this->assertObjectHasProperty('superfield', $profilefields1);
$this->assertEquals('Loves cats', $profilefields1->superfield);
}

Expand Down
4 changes: 2 additions & 2 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ public function test_badge_awards() {
$message = array_pop($messages);
// Check we have the expected data.
$customdata = json_decode($message->customdata);
$this->assertObjectHasAttribute('notificationiconurl', $customdata);
$this->assertObjectHasAttribute('hash', $customdata);
$this->assertObjectHasProperty('notificationiconurl', $customdata);
$this->assertObjectHasProperty('hash', $customdata);

$user2 = $this->getDataGenerator()->create_user();
$badge->issue($user2->id, true);
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 @@ -711,7 +711,7 @@ public function test_get_functions_return_custom_fields() {

// Test cohort_get_cohort.
$result = cohort_get_cohort($cohort1->id, $coursectx, true);
$this->assertObjectHasAttribute('customfields', $result);
$this->assertObjectHasProperty('customfields', $result);
$this->assertCount(1, $result->customfields);
$field = reset($result->customfields);
$this->assertInstanceOf(data_controller::class, $field);
Expand All @@ -727,7 +727,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->assertObjectHasAttribute('customfields', $cohort);
$this->assertObjectHasProperty('customfields', $cohort);
$this->assertCount(1, $cohort->customfields);
$field = reset($cohort->customfields);
$this->assertInstanceOf(data_controller::class, $field);
Expand All @@ -753,7 +753,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->assertObjectHasAttribute('customfields', $cohort);
$this->assertObjectHasProperty('customfields', $cohort);
$this->assertCount(1, $cohort->customfields);
$field = reset($cohort->customfields);
$this->assertInstanceOf(data_controller::class, $field);
Expand All @@ -778,7 +778,7 @@ public function test_get_functions_return_custom_fields() {
$result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, '', true);
$this->assertCount(2, $result);
foreach ($result as $cohort) {
$this->assertObjectHasAttribute('customfields', $cohort);
$this->assertObjectHasProperty('customfields', $cohort);
$this->assertCount(1, $cohort->customfields);
$field = reset($cohort->customfields);
$this->assertInstanceOf(data_controller::class, $field);
Expand All @@ -805,7 +805,7 @@ public function test_get_functions_return_custom_fields() {
$result = cohort_get_user_cohorts($user->id, true);
$this->assertCount(2, $result);
foreach ($result as $cohort) {
$this->assertObjectHasAttribute('customfields', $cohort);
$this->assertObjectHasProperty('customfields', $cohort);
$this->assertCount(1, $cohort->customfields);
$field = reset($cohort->customfields);
$this->assertInstanceOf(data_controller::class, $field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,6 @@ public function test_create_matrix_user_profile_fields(): void {
$this->assertNotFalse($matrixprofilefield);

$user = $this->getDataGenerator()->create_user();
$this->assertObjectHasAttribute($matrixprofilefield, profile_user_record($user->id));
$this->assertObjectHasProperty($matrixprofilefield, profile_user_record($user->id));
}
}
4 changes: 2 additions & 2 deletions competency/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function test_comment_add_user_competency() {
$this->assertEquals($expectedurlname, $message->contexturlname);
// Test customdata.
$customdata = json_decode($message->customdata);
$this->assertObjectHasAttribute('notificationiconurl', $customdata);
$this->assertObjectHasProperty('notificationiconurl', $customdata);
$this->assertStringContainsString('tokenpluginfile.php', $customdata->notificationiconurl);
$userpicture = new \user_picture($u1);
$userpicture->size = 1; // Use f1 size.
Expand Down Expand Up @@ -229,7 +229,7 @@ public function test_comment_add_plan() {
$this->assertEquals($u1->id, $message->useridto);
// Test customdata.
$customdata = json_decode($message->customdata);
$this->assertObjectHasAttribute('notificationiconurl', $customdata);
$this->assertObjectHasProperty('notificationiconurl', $customdata);

// Post a comment in a plan with reviewer. The reviewer is messaged.
$p1->set('reviewerid', $u2->id);
Expand Down
36 changes: 18 additions & 18 deletions course/tests/exporters_content_item_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public function test_export_course_content_item() {
$renderer = $PAGE->get_renderer('core');
$exporteditem = $ciexporter->export($renderer);

$this->assertObjectHasAttribute('id', $exporteditem);
$this->assertObjectHasProperty('id', $exporteditem);
$this->assertEquals($exporteditem->id, $contentitem->get_id());
$this->assertObjectHasAttribute('name', $exporteditem);
$this->assertObjectHasProperty('name', $exporteditem);
$this->assertEquals($exporteditem->name, $contentitem->get_name());
$this->assertObjectHasAttribute('title', $exporteditem);
$this->assertObjectHasProperty('title', $exporteditem);
$this->assertEquals($exporteditem->title, $contentitem->get_title()->get_value());
$this->assertObjectHasAttribute('link', $exporteditem);
$this->assertObjectHasProperty('link', $exporteditem);
$this->assertEquals($exporteditem->link, $contentitem->get_link()->out(false));
$this->assertObjectHasAttribute('icon', $exporteditem);
$this->assertObjectHasProperty('icon', $exporteditem);
$this->assertEquals($exporteditem->icon, $contentitem->get_icon());
$this->assertObjectHasAttribute('help', $exporteditem);
$this->assertObjectHasProperty('help', $exporteditem);
$this->assertEquals($exporteditem->help, format_text($contentitem->get_help(), FORMAT_MARKDOWN));
$this->assertObjectHasAttribute('archetype', $exporteditem);
$this->assertObjectHasProperty('archetype', $exporteditem);
$this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
$this->assertObjectHasAttribute('componentname', $exporteditem);
$this->assertObjectHasProperty('componentname', $exporteditem);
$this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
$this->assertObjectHasAttribute('legacyitem', $exporteditem);
$this->assertObjectHasProperty('legacyitem', $exporteditem);
$this->assertFalse($exporteditem->legacyitem);
$this->assertEquals($exporteditem->purpose, $contentitem->get_purpose());
$this->assertEquals($exporteditem->branded, $contentitem->is_branded());
Expand Down Expand Up @@ -102,24 +102,24 @@ public function test_export_course_content_item_legacy() {
$renderer = $PAGE->get_renderer('core');
$exporteditem = $ciexporter->export($renderer);

$this->assertObjectHasAttribute('id', $exporteditem);
$this->assertObjectHasProperty('id', $exporteditem);
$this->assertEquals($exporteditem->id, $contentitem->get_id());
$this->assertObjectHasAttribute('name', $exporteditem);
$this->assertObjectHasProperty('name', $exporteditem);
$this->assertEquals($exporteditem->name, $contentitem->get_name());
$this->assertObjectHasAttribute('title', $exporteditem);
$this->assertObjectHasProperty('title', $exporteditem);
$this->assertEquals($exporteditem->title, $contentitem->get_title()->get_value());
$this->assertObjectHasAttribute('link', $exporteditem);
$this->assertObjectHasProperty('link', $exporteditem);
$this->assertEquals($exporteditem->link, $contentitem->get_link()->out(false));
$this->assertObjectHasAttribute('icon', $exporteditem);
$this->assertObjectHasProperty('icon', $exporteditem);
$this->assertEquals($exporteditem->icon, $contentitem->get_icon());
$this->assertObjectHasAttribute('help', $exporteditem);
$this->assertObjectHasProperty('help', $exporteditem);
$this->assertEquals($exporteditem->help, format_text($contentitem->get_help(), FORMAT_MARKDOWN));
$this->assertObjectHasAttribute('archetype', $exporteditem);
$this->assertObjectHasProperty('archetype', $exporteditem);
$this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
$this->assertObjectHasAttribute('componentname', $exporteditem);
$this->assertObjectHasProperty('componentname', $exporteditem);
$this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
// Most important, is this a legacy item?
$this->assertObjectHasAttribute('legacyitem', $exporteditem);
$this->assertObjectHasProperty('legacyitem', $exporteditem);
$this->assertTrue($exporteditem->legacyitem);
}
}
18 changes: 9 additions & 9 deletions course/tests/exporters_content_items_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public function test_export_course_content_items() {
$renderer = $PAGE->get_renderer('core');
$exportedcontentitems = $ciexporter->export($renderer);

$this->assertObjectHasAttribute('content_items', $exportedcontentitems);
$this->assertObjectHasProperty('content_items', $exportedcontentitems);
foreach ($exportedcontentitems->content_items as $key => $dto) {
$this->assertObjectHasAttribute('id', $dto);
$this->assertObjectHasAttribute('name', $dto);
$this->assertObjectHasAttribute('title', $dto);
$this->assertObjectHasAttribute('link', $dto);
$this->assertObjectHasAttribute('icon', $dto);
$this->assertObjectHasAttribute('help', $dto);
$this->assertObjectHasAttribute('archetype', $dto);
$this->assertObjectHasAttribute('componentname', $dto);
$this->assertObjectHasProperty('id', $dto);
$this->assertObjectHasProperty('name', $dto);
$this->assertObjectHasProperty('title', $dto);
$this->assertObjectHasProperty('link', $dto);
$this->assertObjectHasProperty('icon', $dto);
$this->assertObjectHasProperty('help', $dto);
$this->assertObjectHasProperty('archetype', $dto);
$this->assertObjectHasProperty('componentname', $dto);
}
}
}
16 changes: 8 additions & 8 deletions course/tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ public function test_export_context_data_module_context_only() {
$writer = \core_privacy\local\request\writer::with_context($context1);
$this->assertTrue($writer->has_any_data());
$writerdata = $writer->get_data();
$this->assertObjectHasAttribute('fullname', $writerdata);
$this->assertObjectHasAttribute('shortname', $writerdata);
$this->assertObjectHasAttribute('idnumber', $writerdata);
$this->assertObjectHasAttribute('summary', $writerdata);
$this->assertObjectHasProperty('fullname', $writerdata);
$this->assertObjectHasProperty('shortname', $writerdata);
$this->assertObjectHasProperty('idnumber', $writerdata);
$this->assertObjectHasProperty('summary', $writerdata);
}

/**
Expand Down Expand Up @@ -226,10 +226,10 @@ public function test_export_context_data_course_and_module_contexts() {
$writer = \core_privacy\local\request\writer::with_context($context1);
$this->assertTrue($writer->has_any_data());
$writerdata = $writer->get_data();
$this->assertObjectHasAttribute('fullname', $writerdata);
$this->assertObjectHasAttribute('shortname', $writerdata);
$this->assertObjectHasAttribute('idnumber', $writerdata);
$this->assertObjectHasAttribute('summary', $writerdata);
$this->assertObjectHasProperty('fullname', $writerdata);
$this->assertObjectHasProperty('shortname', $writerdata);
$this->assertObjectHasProperty('idnumber', $writerdata);
$this->assertObjectHasProperty('summary', $writerdata);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions course/tests/services_content_item_service_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public function test_get_content_items_for_user_in_course_basic() {
$contentitems = $cis->get_content_items_for_user_in_course($user, $course);

foreach ($contentitems as $key => $contentitem) {
$this->assertObjectHasAttribute('id', $contentitem);
$this->assertObjectHasAttribute('name', $contentitem);
$this->assertObjectHasAttribute('title', $contentitem);
$this->assertObjectHasAttribute('link', $contentitem);
$this->assertObjectHasAttribute('icon', $contentitem);
$this->assertObjectHasAttribute('help', $contentitem);
$this->assertObjectHasAttribute('archetype', $contentitem);
$this->assertObjectHasAttribute('componentname', $contentitem);
$this->assertObjectHasProperty('id', $contentitem);
$this->assertObjectHasProperty('name', $contentitem);
$this->assertObjectHasProperty('title', $contentitem);
$this->assertObjectHasProperty('link', $contentitem);
$this->assertObjectHasProperty('icon', $contentitem);
$this->assertObjectHasProperty('help', $contentitem);
$this->assertObjectHasProperty('archetype', $contentitem);
$this->assertObjectHasProperty('componentname', $contentitem);
}
}

Expand Down
4 changes: 2 additions & 2 deletions course/tests/task/content_notification_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function test_execute(): void {

$messagecustomdata = json_decode($message->customdata);
$this->assertEquals($course->id, $messagecustomdata->courseid);
$this->assertObjectHasAttribute('notificationiconurl', $messagecustomdata);
$this->assertObjectHasAttribute('notificationpictureurl', $messagecustomdata);
$this->assertObjectHasProperty('notificationiconurl', $messagecustomdata);
$this->assertObjectHasProperty('notificationpictureurl', $messagecustomdata);
}

// Now, set the course to not visible.
Expand Down
18 changes: 9 additions & 9 deletions enrol/tests/enrollib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,26 +952,26 @@ public function test_enrol_get_my_courses_all_accessible() {
$this->assertEquals([$course1->id, $course2->id, $course3->id], array_keys($courses));

// Check fields parameter still works. Fields default (certain base fields).
$this->assertObjectHasAttribute('id', $courses[$course3->id]);
$this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
$this->assertObjectHasProperty('id', $courses[$course3->id]);
$this->assertObjectHasProperty('shortname', $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->assertObjectHasProperty('id', $courses[$course3->id]);
$this->assertObjectHasProperty('shortname', $courses[$course3->id]);
$this->assertObjectHasProperty('summary', $courses[$course3->id]);
$this->assertObjectNotHasProperty('summaryformat', $courses[$course3->id]);

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

// Specified fields (two, array).
$courses = enrol_get_my_courses(['summary', 'summaryformat'], 'id', 0, [], true);
$this->assertObjectHasAttribute('summary', $courses[$course3->id]);
$this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
$this->assertObjectHasProperty('summary', $courses[$course3->id]);
$this->assertObjectHasProperty('summaryformat', $courses[$course3->id]);

// By default, courses are ordered by sortorder - which by default is most recent first.
$courses = enrol_get_my_courses(null, null, 0, [], true);
Expand Down
16 changes: 8 additions & 8 deletions favourites/tests/repository_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public function test_add() {

// Verify we get the record back.
$this->assertInstanceOf(favourite::class, $favourite);
$this->assertObjectHasAttribute('id', $favourite);
$this->assertObjectHasProperty('id', $favourite);
$this->assertEquals('core_course', $favourite->component);
$this->assertEquals('course', $favourite->itemtype);

// Verify the returned object has additional properties, created as part of the add.
$this->assertObjectHasAttribute('ordering', $favourite);
$this->assertObjectHasAttribute('timecreated', $favourite);
$this->assertObjectHasProperty('ordering', $favourite);
$this->assertObjectHasProperty('timecreated', $favourite);
$this->assertGreaterThanOrEqual($timenow, $favourite->timecreated);

// Try to save the same record again and confirm the store throws an exception.
Expand Down Expand Up @@ -137,8 +137,8 @@ public function test_add_all_basic() {
$this->assertEquals('course', $favourite->itemtype);

// Verify the returned object has additional properties, created as part of the add.
$this->assertObjectHasAttribute('ordering', $favourite);
$this->assertObjectHasAttribute('timecreated', $favourite);
$this->assertObjectHasProperty('ordering', $favourite);
$this->assertObjectHasProperty('timecreated', $favourite);
$this->assertGreaterThanOrEqual($timenow, $favourite->timecreated);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ public function test_find() {
// Now, from the repo, get the single favourite we just created, by id.
$userfavourite = $favouritesrepo->find($favourite->id);
$this->assertInstanceOf(favourite::class, $userfavourite);
$this->assertObjectHasAttribute('timecreated', $userfavourite);
$this->assertObjectHasProperty('timecreated', $userfavourite);

// Try to get a favourite we know doesn't exist.
// We expect an exception in this case.
Expand Down Expand Up @@ -209,8 +209,8 @@ public function test_find_all() {
$this->assertCount(4, $favourites);
foreach ($favourites as $fav) {
$this->assertInstanceOf(favourite::class, $fav);
$this->assertObjectHasAttribute('id', $fav);
$this->assertObjectHasAttribute('timecreated', $fav);
$this->assertObjectHasProperty('id', $fav);
$this->assertObjectHasProperty('timecreated', $fav);
}
}

Expand Down
Loading

0 comments on commit 08027e4

Please sign in to comment.