MDL-61667 analytics: Fix checking that a given model does not exist
authorDavid Mudrák <david@moodle.com>
Tue, 12 Mar 2019 08:09:26 +0000 (09:09 +0100)
committerDavid Mudrák <david@moodle.com>
Mon, 1 Apr 2019 12:23:06 +0000 (14:23 +0200)
The original implementation did not work well for checking that the
given model does NOT exist. If no record was found in the
analytics_models table, the code execution continued and it reached the
moment when indicators were checked. If no indicators were provided, the
call ended up with error 'array_keys() expects parameter 1 to be array,
boolean given' (because indicators were set to false).

analytics/classes/model.php
analytics/tests/manager_test.php
analytics/tests/model_test.php

index a96e6a6..5583535 100644 (file)
@@ -407,6 +407,10 @@ class model {
 
         $existingmodels = $DB->get_records('analytics_models', array('target' => $target->get_id()));
 
+        if (!$existingmodels) {
+            return false;
+        }
+
         if (!$indicators && $existingmodels) {
             return true;
         }
index ab5708a..7034461 100644 (file)
@@ -281,6 +281,7 @@ class analytics_manager_testcase extends advanced_testcase {
         // Check that no such model exists yet.
         $target = \core_analytics\manager::get_target('test_target_course_level_shortname');
         $this->assertEquals(0, $DB->count_records('analytics_models', ['target' => $target->get_id()]));
+        $this->assertFalse(\core_analytics\model::exists($target));
 
         // Check that the model is created.
         $created = \core_analytics\manager::create_declared_model($declaration);
index 8eaac08..0e3385a 100644 (file)
@@ -274,6 +274,12 @@ class analytics_model_testcase extends advanced_testcase {
 
         $target = \core_analytics\manager::get_target('\core\analytics\target\no_teaching');
         $this->assertTrue(\core_analytics\model::exists($target));
+
+        foreach (\core_analytics\manager::get_all_models() as $model) {
+            $model->delete();
+        }
+
+        $this->assertFalse(\core_analytics\model::exists($target));
     }
 
     /**