MDL-61364 core_question: tests for question_has_capability_on exception
authorSimey Lameze <simey@moodle.com>
Tue, 27 Mar 2018 08:47:59 +0000 (16:47 +0800)
committerSimey Lameze <simey@moodle.com>
Wed, 28 Mar 2018 00:54:11 +0000 (08:54 +0800)
lib/tests/questionlib_test.php

index 7568f6d..b946524 100644 (file)
@@ -1890,7 +1890,7 @@ class core_questionlib_testcase extends advanced_testcase {
      * @dataProvider question_capability_on_question_provider
      * @param   array   $capabilities The capability assignments to set.
      * @param   string  $capability The capability to test
-     * @param   bool    $expectall The expectation when passing false to checkmine.
+     * @param   bool    $isowner The expectation when passing false to checkmine.
      * @param   bool    $expectmine The expectation when passing true to checkmine.
      */
     public function test_question_has_capability_on_using_question($capabilities, $capability, $isowner, $expect) {
@@ -1930,4 +1930,37 @@ class core_questionlib_testcase extends advanced_testcase {
         $result = question_has_capability_on($question, $capability);
         $this->assertEquals($expect, $result);
     }
+
+    /**
+     * Tests that question_has_capability_on throws an exception for wrong parameter types.
+     */
+    public function test_question_has_capability_on_wrong_param_type() {
+        // Create the test data.
+        $generator = $this->getDataGenerator();
+        $questiongenerator = $generator->get_plugin_generator('core_question');
+        $user = $generator->create_user();
+
+        $category = $generator->create_category();
+        $context = context_coursecat::instance($category->id);
+        $questioncat = $questiongenerator->create_question_category([
+            'contextid' => $context->id,
+        ]);
+
+        // Create the question.
+        $question = $questiongenerator->create_question('truefalse', null, [
+            'category' => $questioncat->id,
+        ]);
+        $question = question_bank::load_question_data($question->id);
+
+        // The question generator does not support setting of the createdby for some reason.
+        $question->createdby = $user->id;
+
+        $this->setUser($user);
+        $result = question_has_capability_on((string)$question->id, 'tag');
+        $this->assertFalse($result);
+
+        $this->expectException('coding_exception');
+        $this->expectExceptionMessage('$questionorid parameter needs to be an integer or an object.');
+        question_has_capability_on('one', 'tag');
+    }
 }