Merge branch 'wip_MDL-46552_m27_memcached' of https://github.com/skodak/moodle into...
[moodle.git] / lib / tests / conditionlib_test.php
blobf2b036b3ea147d31410a3f27ae1e1bda127dc19b
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.
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/>.
17 /**
18 * Tests for deprecated conditional activities classes.
20 * @package core_availability
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/lib/conditionlib.php');
31 /**
32 * Tests for deprecated conditional activities classes.
34 * @package core_availability
35 * @copyright 2014 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
38 class core_conditionlib_testcase extends advanced_testcase {
40 protected function setUp() {
41 global $CFG;
42 parent::setUp();
44 $this->resetAfterTest();
46 $CFG->enableavailability = 1;
47 $CFG->enablecompletion = 1;
48 $user = $this->getDataGenerator()->create_user();
49 $this->setUser($user);
52 public function test_constructor() {
53 $generator = $this->getDataGenerator();
54 $course = $generator->create_course();
55 $page = $generator->get_plugin_generator('mod_page')->create_instance(
56 array('course' => $course));
57 $modinfo = get_fast_modinfo($course);
59 // No ID.
60 try {
61 $test = new condition_info((object)array());
62 $this->fail();
63 } catch (coding_exception $e) {
64 // Do nothing.
65 $this->assertDebuggingCalled();
68 // Get actual cm_info for comparison.
69 $realcm = $modinfo->get_cm($page->cmid);
71 // No other data.
72 $test = new condition_info((object)array('id' => $page->cmid));
73 $this->assertDebuggingCalled();
74 $this->assertEquals($realcm, $test->get_full_course_module());
75 $this->assertDebuggingCalled();
77 // Course id.
78 $test = new condition_info((object)array('id' => $page->cmid, 'course' => $course->id));
79 $this->assertDebuggingCalled();
80 $this->assertEquals($realcm, $test->get_full_course_module());
81 $this->assertDebuggingCalled();
83 // Full cm.
84 $test = new condition_info($realcm);
85 $this->assertDebuggingCalled();
86 $this->assertEquals($realcm, $test->get_full_course_module());
87 $this->assertDebuggingCalled();
90 /**
91 * Same as above test but for course_sections instead of course_modules.
93 public function test_section_constructor() {
94 $generator = $this->getDataGenerator();
95 $course = $generator->create_course(
96 array('numsections' => 1), array('createsections' => true));
97 $modinfo = get_fast_modinfo($course);
99 // No ID.
100 try {
101 $test = new condition_info_section(((object)array()));
102 $this->fail();
103 } catch (coding_exception $e) {
104 // Do nothing.
105 $this->assertDebuggingCalled();
108 // Get actual cm_info for comparison.
109 $realsection = $modinfo->get_section_info(1);
111 // No other data.
112 $test = new condition_info_section((object)array('id' => $realsection->id));
113 $this->assertDebuggingCalled();
114 $this->assertEquals($realsection, $test->get_full_section());
115 $this->assertDebuggingCalled();
117 // Course id.
118 $test = new condition_info_section((object)array('id' => $realsection->id,
119 'course' => $course->id));
120 $this->assertDebuggingCalled();
121 $this->assertEquals($realsection, $test->get_full_section());
122 $this->assertDebuggingCalled();
124 // Full object.
125 $test = new condition_info_section($realsection);
126 $this->assertDebuggingCalled();
127 $this->assertEquals($realsection, $test->get_full_section());
128 $this->assertDebuggingCalled();
132 * Tests the is_available function for modules. This does not test all the
133 * conditions and stuff, because it only needs to check that the system
134 * connects through to the real availability API. Also tests
135 * get_full_information function.
137 public function test_is_available() {
138 // Create course.
139 $generator = $this->getDataGenerator();
140 $course = $generator->create_course();
142 // Create activity with no restrictions and one with date restriction.
143 $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
144 array('course' => $course));
145 $time = time() + 100;
146 $avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
147 $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
148 array('course' => $course, 'availability' => $avail));
150 // No conditions.
151 $ci = new condition_info((object)array('id' => $page1->cmid),
152 CONDITION_MISSING_EVERYTHING);
153 $this->assertDebuggingCalled();
154 $this->assertTrue($ci->is_available($text, false, 0));
155 $this->assertDebuggingCalled();
156 $this->assertEquals('', $text);
158 // Date condition.
159 $ci = new condition_info((object)array('id' => $page2->cmid),
160 CONDITION_MISSING_EVERYTHING);
161 $this->assertDebuggingCalled();
162 $this->assertFalse($ci->is_available($text));
163 $this->assertDebuggingCalled();
164 $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
165 $this->assertContains($expectedtime, $text);
167 // Full information display.
168 $text = $ci->get_full_information();
169 $this->assertDebuggingCalled();
170 $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
171 $this->assertContains($expectedtime, $text);
175 * Tests the is_available function for sections.
177 public function test_section_is_available() {
178 global $DB;
180 // Create course.
181 $generator = $this->getDataGenerator();
182 $course = $generator->create_course(
183 array('numsections' => 2), array('createsections' => true));
185 // Set one of the sections unavailable.
186 $time = time() + 100;
187 $avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
188 $DB->set_field('course_sections', 'availability', $avail, array(
189 'course' => $course->id, 'section' => 2));
191 $modinfo = get_fast_modinfo($course);
193 // No conditions.
194 $ci = new condition_info_section($modinfo->get_section_info(1));
195 $this->assertDebuggingCalled();
196 $this->assertTrue($ci->is_available($text, false, 0));
197 $this->assertDebuggingCalled();
198 $this->assertEquals('', $text);
200 // Date condition.
201 $ci = new condition_info_section($modinfo->get_section_info(2));
202 $this->assertDebuggingCalled();
203 $this->assertFalse($ci->is_available($text));
204 $this->assertDebuggingCalled();
205 $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
206 $this->assertContains($expectedtime, $text);
208 // Full information display.
209 $text = $ci->get_full_information();
210 $this->assertDebuggingCalled();
211 $expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
212 $this->assertContains($expectedtime, $text);