Merge branch 'MDL-63214-master' of git://github.com/sarjona/moodle
[moodle.git] / repository / tests / generator_test.php
blob2f2f30923bbf954fdbee32741c2aade394d1c9b7
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 * Repository generator tests
20 * @package repository
21 * @category test
22 * @copyright 2013 Frédéric Massart
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Repository generator tests class
31 * @package repository
32 * @category test
33 * @copyright 2013 Frédéric Massart
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_repository_generator_testcase extends advanced_testcase {
38 /**
39 * Basic test of creation of repository types.
41 * @return void
43 public function test_create_type() {
44 global $DB;
45 $this->resetAfterTest(true);
47 // All the repository types.
48 $all = array('boxnet', 'coursefiles', 'dropbox', 'equella', 'filesystem', 'flickr',
49 'flickr_public', 'googledocs', 'local', 'merlot', 'picasa', 'recent', 's3', 'upload', 'url',
50 'user', 'webdav', 'wikimedia', 'youtube');
52 // The ones enabled during installation.
53 $alreadyenabled = array('local', 'recent', 'upload', 'url', 'user', 'wikimedia');
55 // Enable all the repositories which are not enabled yet.
56 foreach ($all as $type) {
57 if (in_array($type, $alreadyenabled)) {
58 continue;
60 $repotype = $this->getDataGenerator()->create_repository_type($type);
61 $this->assertEquals($repotype->type, $type, 'Unexpected name after creating repository type ' . $type);
62 $this->assertTrue($DB->record_exists('repository', array('type' => $type, 'visible' => 1)));
65 // Check that all the repositories have been enabled.
66 foreach ($all as $type) {
67 $caughtexception = false;
68 try {
69 $this->getDataGenerator()->create_repository_type($type);
70 } catch (repository_exception $e) {
71 if ($e->getMessage() === 'This repository already exists') {
72 $caughtexception = true;
75 $this->assertTrue($caughtexception, "Repository type '$type' should have already been enabled");
79 /**
80 * Ensure that the type options are properly saved.
82 * @return void
84 public function test_create_type_custom_options() {
85 global $DB;
86 $this->resetAfterTest(true);
88 // Single instances.
89 // Note: for single instances repositories enablecourseinstances and enableuserinstances are forced set to 0.
90 $record = new stdClass();
91 $record->pluginname = 'Custom Flickr';
92 $record->api_key = '12345';
93 $record->secret = '67890';
94 $flickr = $this->getDataGenerator()->create_repository_type('flickr', $record);
96 $config = get_config('flickr');
97 $record->enableuserinstances = '0';
98 $record->enablecourseinstances = '0';
99 $this->assertEquals($record, $config);
100 $this->assertEquals('Custom Flickr',
101 $DB->get_field('repository_instances', 'name', array('typeid' => $flickr->id), MUST_EXIST));
103 $record = new stdClass();
104 $record->pluginname = 'Custom Dropbox';
105 $record->dropbox_key = '12345';
106 $record->dropbox_secret = '67890';
107 $record->dropbox_cachelimit = '123';
108 $dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record);
110 $config = get_config('dropbox');
111 $record->enableuserinstances = '0';
112 $record->enablecourseinstances = '0';
113 $this->assertEquals($record, $config);
114 $this->assertEquals('Custom Dropbox',
115 $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST));
117 // Multiple instances.
118 $record = new stdClass();
119 $record->pluginname = 'Custom WebDAV';
120 $record->enableuserinstances = '0';
121 $record->enablecourseinstances = '0';
122 $webdav = $this->getDataGenerator()->create_repository_type('webdav', $record);
124 $config = get_config('webdav');
125 $this->assertEquals($record, $config);
126 $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $webdav->id)));
128 $record = new stdClass();
129 $record->pluginname = 'Custom Equella';
130 $record->enableuserinstances = '1';
131 $record->enablecourseinstances = '0';
132 $equella = $this->getDataGenerator()->create_repository_type('equella', $record);
134 $config = get_config('equella');
135 $this->assertEquals($record, $config);
136 $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $equella->id)));
140 * Covers basic testing of instance creation.
142 * @return void
144 public function test_create_instance() {
145 global $DB;
146 $this->resetAfterTest(true);
148 $course = $this->getDataGenerator()->create_course();
149 $user = $this->getDataGenerator()->create_user();
150 $block = $this->getDataGenerator()->create_block('online_users');
152 $type = $this->getDataGenerator()->create_repository_type('webdav');
153 $record = new stdClass();
154 $record->name = 'A WebDAV instance';
155 $record->webdav_type = '1';
156 $record->webdav_server = 'localhost';
157 $record->webdav_port = '12345';
158 $record->webdav_path = '/nothing';
159 $record->webdav_user = 'me';
160 $record->webdav_password = '\o/';
161 $record->webdav_auth = 'basic';
162 $instance = $this->getDataGenerator()->create_repository('webdav', $record);
164 $this->assertEquals(1, $DB->count_records('repository_instances', array('typeid' => $type->id)));
165 $this->assertEquals($record->name, $DB->get_field('repository_instances', 'name', array('id' => $instance->id)));
166 $entries = $DB->get_records('repository_instance_config', array('instanceid' => $instance->id));
167 $config = new stdClass();
168 foreach ($entries as $entry) {
169 $config->{$entry->name} = $entry->value;
171 unset($record->name);
172 $this->assertEquals($config, $record);
174 // Course context.
175 $record = new stdClass();
176 $record->contextid = context_course::instance($course->id)->id;
177 $instance = $this->getDataGenerator()->create_repository('webdav', $record);
178 $this->assertEquals(2, $DB->count_records('repository_instances', array('typeid' => $type->id)));
179 $this->assertEquals($record->contextid, $instance->contextid);
181 // User context.
182 $record->contextid = context_user::instance($user->id)->id;
183 $instance = $this->getDataGenerator()->create_repository('webdav', $record);
184 $this->assertEquals(3, $DB->count_records('repository_instances', array('typeid' => $type->id)));
185 $this->assertEquals($record->contextid, $instance->contextid);
187 // Invalid context.
188 $this->expectException('coding_exception');
189 $record->contextid = context_block::instance($block->id)->id;
190 $instance = $this->getDataGenerator()->create_repository('webdav', $record);