unit tests: MDL-17646 Helper functions for loading and cleaning up test data.
[moodle.git] / lib / simpletest / testsimpletestlib.php
CommitLineData
821e4ecf 1<?php
2/**
3 * Unit tests for (some of) ../accesslib.php.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package moodlecore
9 */
10
11if (!defined('MOODLE_INTERNAL')) {
12 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
13}
14
15class accesslib_test extends MoodleUnitTestCase {
16
17 function test_get_parent_contexts() {
18 $context = get_context_instance(CONTEXT_SYSTEM);
19 $this->assertEqual(get_parent_contexts($context), array());
20
21 $context = new stdClass;
22 $context->path = '/1/25';
23 $this->assertEqual(get_parent_contexts($context), array(1));
24
25 $context = new stdClass;
26 $context->path = '/1/123/234/345/456';
27 $this->assertEqual(get_parent_contexts($context), array(345, 234, 123, 1));
28 }
29
30 function test_get_parent_contextid() {
31 $context = get_context_instance(CONTEXT_SYSTEM);
32 $this->assertFalse(get_parent_contextid($context));
33
34 $context = new stdClass;
35 $context->path = '/1/25';
36 $this->assertEqual(get_parent_contextid($context), 1);
37
38 $context = new stdClass;
39 $context->path = '/1/123/234/345/456';
40 $this->assertEqual(get_parent_contextid($context), 345);
41 }
42
43 function test_get_users_by_capability() {
44 global $DB;
45 // Create three nested contexts. instanceid does not matter for this. Just
46 // ensure we don't violate any unique keys by using an unlikely number.
47 // We will fix paths in a second.
48 $contexts = $this->load_test_data('context',
49 array('contextlevel', 'instanceid', 'path', 'depth'), array(
50 array(10, 666, '', 1),
51 array(40, 666, '', 2),
52 array(50, 666, '', 3),
53 ));
54 $contexts[0]->path = '/' . $contexts[0]->id;
55 $DB->set_field('context', 'path', $contexts[0]->path, array('id' => $contexts[0]->id));
56 $contexts[1]->path = $contexts[0]->path . '/' . $contexts[1]->id;
57 $DB->set_field('context', 'path', $contexts[1]->path, array('id' => $contexts[1]->id));
58 $contexts[2]->path = $contexts[1]->path . '/' . $contexts[2]->id;
59 $DB->set_field('context', 'path', $contexts[2]->path, array('id' => $contexts[2]->id));
60
61 // Just test load_test_data and delete_test_data for now.
62 $this->assertTrue($DB->record_exists('context', array('id' => $contexts[1]->id)));
63 $this->assertTrue($DB->get_field('context', 'path', array('id' => $contexts[2]->id)), $contexts[2]->path);
64 $this->delete_test_data('context', $contexts);
65 $this->assertFalse($DB->record_exists('context', array('id' => $contexts[1]->id)));
66 }
67}
68?>