MDL-62781 question/privacy: fix tests with CodeRunner is installed
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 22 Jun 2018 09:45:56 +0000 (10:45 +0100)
committerJun Pataleta <jun@moodle.com>
Wed, 18 Jul 2018 01:57:49 +0000 (09:57 +0800)
The tests were assuming that no plugin ever created questions on
install.

Also change the tests to do the counting in the DB. That is more
efficient.

question/tests/privacy_provider_test.php

index 6760e7d..7b6ffca 100644 (file)
@@ -261,15 +261,18 @@ class core_question_privacy_provider_testcase extends \core_privacy\tests\provid
         // Run the delete functions as default user.
         $this->setUser();
 
         // Run the delete functions as default user.
         $this->setUser();
 
+        // Find out how many questions are in the question bank to start with.
+        $questioncount = $DB->count_records('question');
+
         // The delete functions should do nothing here.
         // The delete functions should do nothing here.
-        $this->assertCount(6, $DB->get_records('question'));
+        $this->assertEquals($questioncount, $DB->count_records('question'));
 
         // Delete for all users in context.
         provider::delete_data_for_all_users_in_context($expectedcontext);
 
         // Delete for all users in context.
         provider::delete_data_for_all_users_in_context($expectedcontext);
-        $this->assertCount(6, $DB->get_records('question'));
+        $this->assertEquals($questioncount, $DB->count_records('question'));
 
         provider::delete_data_for_user($approvedcontextlist);
 
         provider::delete_data_for_user($approvedcontextlist);
-        $this->assertCount(6, $DB->get_records('question'));
+        $this->assertEquals($questioncount, $DB->count_records('question'));
     }
 
     /**
     }
 
     /**
@@ -321,11 +324,14 @@ class core_question_privacy_provider_testcase extends \core_privacy\tests\provid
             [$context->id]
         );
 
             [$context->id]
         );
 
+        // Find out how many questions are in the question bank to start with.
+        $questioncount = $DB->count_records('question');
+
         // Delete the data and check it is removed.
         $this->setUser();
         provider::delete_data_for_user($approvedcontextlist);
 
         // Delete the data and check it is removed.
         $this->setUser();
         provider::delete_data_for_user($approvedcontextlist);
 
-        $this->assertCount(5, $DB->get_records('question'));
+        $this->assertEquals($questioncount, $DB->count_records('question'));
 
         $qrecord = $DB->get_record('question', ['id' => $q1->id]);
         $this->assertEquals(0, $qrecord->createdby);
 
         $qrecord = $DB->get_record('question', ['id' => $q1->id]);
         $this->assertEquals(0, $qrecord->createdby);
@@ -391,11 +397,14 @@ class core_question_privacy_provider_testcase extends \core_privacy\tests\provid
         $questiongenerator->update_question($q3);
         $q5 = $questiongenerator->create_question('shortanswer', null, array('category' => $othercat->id));
 
         $questiongenerator->update_question($q3);
         $q5 = $questiongenerator->create_question('shortanswer', null, array('category' => $othercat->id));
 
+        // Find out how many questions are in the question bank to start with.
+        $questioncount = $DB->count_records('question');
+
         // Delete the data and check it is removed.
         $this->setUser();
         provider::delete_data_for_all_users_in_context($context);
 
         // Delete the data and check it is removed.
         $this->setUser();
         provider::delete_data_for_all_users_in_context($context);
 
-        $this->assertCount(5, $DB->get_records('question'));
+        $this->assertEquals($questioncount, $DB->count_records('question'));
 
         $qrecord = $DB->get_record('question', ['id' => $q1->id]);
         $this->assertEquals(0, $qrecord->createdby);
 
         $qrecord = $DB->get_record('question', ['id' => $q1->id]);
         $this->assertEquals(0, $qrecord->createdby);