|
| 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 | +namespace tool_dynamic_cohorts; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for rule class. |
| 21 | + * |
| 22 | + * @package tool_dynamic_cohorts |
| 23 | + * @copyright 2024 Catalyst IT |
| 24 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + * |
| 26 | + * @covers \tool_dynamic_cohorts\rule |
| 27 | +
|
| 28 | + */ |
| 29 | +class rule_test extends \advanced_testcase { |
| 30 | + |
| 31 | + /** |
| 32 | + * Test is_enabled. |
| 33 | + */ |
| 34 | + public function test_is_enabled() { |
| 35 | + $this->resetAfterTest(); |
| 36 | + |
| 37 | + $rule = new rule(0, (object)['name' => 'Test rule disabled']); |
| 38 | + $this->assertFalse($rule->is_enabled()); |
| 39 | + |
| 40 | + $rule = new rule(0, (object)['name' => 'Test rule enabled', 'enabled' => 1]); |
| 41 | + $this->assertTrue($rule->is_enabled()); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test is_bulk_processing. |
| 46 | + */ |
| 47 | + public function is_bulk_processing() { |
| 48 | + $this->resetAfterTest(); |
| 49 | + |
| 50 | + $rule = new rule(0, (object)['name' => 'Test rule bulk processing', 'bulkprocessing' => 1]); |
| 51 | + $this->assertTrue($rule->is_bulk_processing()); |
| 52 | + |
| 53 | + $rule = new rule(0, (object)['name' => 'Test rule normal']); |
| 54 | + $this->assertFalse($rule->is_bulk_processing()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test getting a list of related condition records. |
| 59 | + */ |
| 60 | + public function test_get_condition_records() { |
| 61 | + $this->resetAfterTest(); |
| 62 | + |
| 63 | + $rule = new rule(0, (object)['name' => 'Test rule with conditions']); |
| 64 | + $rule->save(); |
| 65 | + |
| 66 | + $this->assertEmpty($rule->get_condition_records()); |
| 67 | + |
| 68 | + $condition1 = new condition(0, (object)['ruleid' => $rule->get('id'), 'classname' => 'test', 'sortorder' => 0]); |
| 69 | + $condition1->save(); |
| 70 | + $condition2 = new condition(0, (object)['ruleid' => $rule->get('id'), 'classname' => 'test', 'sortorder' => 1]); |
| 71 | + $condition2->save(); |
| 72 | + |
| 73 | + $actual = $rule->get_condition_records(); |
| 74 | + $this->assertCount(2, $actual); |
| 75 | + |
| 76 | + $this->assertEquals($actual[$condition1->get('id')]->to_record(), $condition1->to_record()); |
| 77 | + $this->assertEquals($actual[$condition2->get('id')]->to_record(), $condition2->to_record()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test is_broken. |
| 82 | + */ |
| 83 | + public function test_is_broken() { |
| 84 | + $this->resetAfterTest(); |
| 85 | + |
| 86 | + $rule = new rule(0, (object)['name' => 'Test rule broken', 'broken' => 1]); |
| 87 | + $this->assertTrue($rule->is_broken()); |
| 88 | + |
| 89 | + $rule = new rule(0, (object)['name' => 'Test not broken']); |
| 90 | + $this->assertFalse($rule->is_broken()); |
| 91 | + } |
| 92 | +} |
0 commit comments