1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Contains unit tests for core_completion/activity_custom_completion. 19 * 20 * @package mod_glossary 21 * @copyright Simey Lameze <simey@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 declare(strict_types=1); 26 27 namespace mod_glossary; 28 29 use advanced_testcase; 30 use cm_info; 31 use coding_exception; 32 use mod_glossary\completion\custom_completion; 33 use moodle_exception; 34 35 defined('MOODLE_INTERNAL') || die(); 36 37 global $CFG; 38 require_once($CFG->libdir . '/completionlib.php'); 39 40 /** 41 * Class for unit testing mod_glossary/activity_custom_completion. 42 * 43 * @package mod_glossary 44 * @copyright Simey Lameze <simey@moodle.com> 45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 46 */ 47 class custom_completion_test extends advanced_testcase { 48 49 /** 50 * Data provider for get_state(). 51 * 52 * @return array[] 53 */ 54 public function get_state_provider(): array { 55 return [ 56 'Undefined rule' => [ 57 'somenonexistentrule', COMPLETION_DISABLED, 0, null, coding_exception::class 58 ], 59 'Rule not available' => [ 60 'completionentries', COMPLETION_DISABLED, 0, null, moodle_exception::class 61 ], 62 'Rule available, user has not submitted' => [ 63 'completionentries', COMPLETION_ENABLED, 0, COMPLETION_INCOMPLETE, null 64 ], 65 'Rule available, user has submitted' => [ 66 'completionentries', COMPLETION_ENABLED, 2, COMPLETION_COMPLETE, null 67 ], 68 ]; 69 } 70 71 /** 72 * Test for get_state(). 73 * 74 * @dataProvider get_state_provider 75 * @param string $rule The custom completion rule. 76 * @param int $available Whether this rule is available. 77 * @param int $entries The number of entries. 78 * @param int|null $status Expected status. 79 * @param string|null $exception Expected exception. 80 */ 81 public function test_get_state(string $rule, int $available, int $entries, ?int $status, ?string $exception) { 82 global $DB; 83 84 if (!is_null($exception)) { 85 $this->expectException($exception); 86 } 87 88 // Custom completion rule data for cm_info::customdata. 89 $customdataval = [ 90 'customcompletionrules' => [ 91 $rule => $available 92 ] 93 ]; 94 95 // Build a mock cm_info instance. 96 $mockcminfo = $this->getMockBuilder(cm_info::class) 97 ->disableOriginalConstructor() 98 ->onlyMethods(['__get']) 99 ->getMock(); 100 101 // Mock the return of the magic getter method when fetching the cm_info object's customdata and instance values. 102 $mockcminfo->expects($this->any()) 103 ->method('__get') 104 ->will($this->returnValueMap([ 105 ['customdata', $customdataval], 106 ['instance', 1], 107 ])); 108 109 // Mock the DB calls. 110 $DB = $this->createMock(get_class($DB)); 111 $DB->expects($this->atMost(1)) 112 ->method('count_records') 113 ->willReturn($entries); 114 115 $customcompletion = new custom_completion($mockcminfo, 2); 116 $this->assertEquals($status, $customcompletion->get_state($rule)); 117 } 118 119 /** 120 * Test for get_defined_custom_rules(). 121 */ 122 public function test_get_defined_custom_rules() { 123 $rules = custom_completion::get_defined_custom_rules(); 124 $this->assertCount(1, $rules); 125 $this->assertEquals('completionentries', $rules[0]); 126 } 127 128 /** 129 * Test for get_defined_custom_rule_descriptions(). 130 */ 131 public function test_get_custom_rule_descriptions() { 132 // Get defined custom rules. 133 $rules = custom_completion::get_defined_custom_rules(); 134 135 // Build a mock cm_info instance. 136 $mockcminfo = $this->getMockBuilder(cm_info::class) 137 ->disableOriginalConstructor() 138 ->onlyMethods(['__get']) 139 ->getMock(); 140 141 // Instantiate a custom_completion object using the mocked cm_info. 142 $customcompletion = new custom_completion($mockcminfo, 1); 143 144 // Get custom rule descriptions. 145 $ruledescriptions = $customcompletion->get_custom_rule_descriptions(); 146 147 // Confirm that defined rules and rule descriptions are consistent with each other. 148 $this->assertEquals(count($rules), count($ruledescriptions)); 149 foreach ($rules as $rule) { 150 $this->assertArrayHasKey($rule, $ruledescriptions); 151 } 152 } 153 154 /** 155 * Test for is_defined(). 156 */ 157 public function test_is_defined() { 158 // Build a mock cm_info instance. 159 $mockcminfo = $this->getMockBuilder(cm_info::class) 160 ->disableOriginalConstructor() 161 ->getMock(); 162 163 $customcompletion = new custom_completion($mockcminfo, 1); 164 165 // Rule is defined. 166 $this->assertTrue($customcompletion->is_defined('completionentries')); 167 168 // Undefined rule. 169 $this->assertFalse($customcompletion->is_defined('somerandomrule')); 170 } 171 172 /** 173 * Data provider for test_get_available_custom_rules(). 174 * 175 * @return array[] 176 */ 177 public function get_available_custom_rules_provider(): array { 178 return [ 179 'Completion submit available' => [ 180 COMPLETION_ENABLED, ['completionentries'] 181 ], 182 'Completion submit not available' => [ 183 COMPLETION_DISABLED, [] 184 ], 185 ]; 186 } 187 188 /** 189 * Test for get_available_custom_rules(). 190 * 191 * @dataProvider get_available_custom_rules_provider 192 * @param int $status 193 * @param array $expected 194 */ 195 public function test_get_available_custom_rules(int $status, array $expected) { 196 $customdataval = [ 197 'customcompletionrules' => [ 198 'completionentries' => $status 199 ] 200 ]; 201 202 // Build a mock cm_info instance. 203 $mockcminfo = $this->getMockBuilder(cm_info::class) 204 ->disableOriginalConstructor() 205 ->onlyMethods(['__get']) 206 ->getMock(); 207 208 // Mock the return of magic getter for the customdata attribute. 209 $mockcminfo->expects($this->any()) 210 ->method('__get') 211 ->with('customdata') 212 ->willReturn($customdataval); 213 214 $customcompletion = new custom_completion($mockcminfo, 1); 215 $this->assertEquals($expected, $customcompletion->get_available_custom_rules()); 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body