@@ -538,18 +538,23 @@ public function test_sync_deletes_tag_instances_for_deleted_metrics(): void {
538538 $ this ->resetAfterTest ();
539539
540540 // Create new metric foo in the database.
541- $ collection = new metric_collection ();
542- $ collection ->add (self ::named_metric_factory (name: 'foo ' ));
543- $ manager = new metrics_manager ($ collection );
544- $ manager ->sync (collect: false , delete: false );
545- $ metric = $ manager ->metrics ['tool_monitoring_foo ' ];
546- self ::assertNotNull ($ metric ->id );
541+ $ data = [
542+ 'component ' => 'tool_monitoring ' ,
543+ 'name ' => 'foo ' ,
544+ 'enabled ' => true ,
545+ 'config ' => null ,
546+ 'timecreated ' => 30 ,
547+ 'timemodified ' => 40 ,
548+ 'usermodified ' => 0 ,
549+ ];
550+ $ metricid = $ DB ->insert_record (registered_metric::TABLE , $ data );
551+ self ::assertNotNull ($ metricid );
547552
548553 // Add tags alpha and beta to metric foo.
549554 core_tag_tag::set_item_tags (
550555 component: 'tool_monitoring ' ,
551556 itemtype: 'metrics ' ,
552- itemid: $ metric -> id ,
557+ itemid: $ metricid ,
553558 context: context_system::instance (),
554559 tagnames: ['alpha ' , 'beta ' ],
555560 );
@@ -558,24 +563,24 @@ public function test_sync_deletes_tag_instances_for_deleted_metrics(): void {
558563 $ DB ->count_records ('tag_instance ' , [
559564 'component ' => 'tool_monitoring ' ,
560565 'itemtype ' => 'metrics ' ,
561- 'itemid ' => $ metric -> id ,
566+ 'itemid ' => $ metricid ,
562567 ]),
563568 );
564569
565570 // Delete metric foo.
566571 $ deletemanager = new metrics_manager (new metric_collection ());
567572 $ deletemanager ->sync (collect: false , delete: true );
568- self ::assertFalse ($ DB ->record_exists (registered_metric::TABLE , ['id ' => $ metric -> id ]));
573+ self ::assertFalse ($ DB ->record_exists (registered_metric::TABLE , ['id ' => $ metricid ]));
569574
570575 // Assert that tags are gone, too.
571576 self ::assertSame (
572577 0 ,
573578 $ DB ->count_records ('tag_instance ' , [
574579 'component ' => 'tool_monitoring ' ,
575580 'itemtype ' => 'metrics ' ,
576- 'itemid ' => $ metric -> id ,
581+ 'itemid ' => $ metricid ,
577582 ]),
578583 );
579- self ::assertSame ([], core_tag_tag::get_item_tags_array ('tool_monitoring ' , 'metrics ' , $ metric -> id ));
584+ self ::assertSame ([], core_tag_tag::get_item_tags_array ('tool_monitoring ' , 'metrics ' , $ metricid ));
580585 }
581586}
0 commit comments