[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Wiki module external functions tests. 19 * 20 * @package mod_wiki 21 * @category external 22 * @copyright 2015 Dani Palou <dani@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.1 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 require_once($CFG->dirroot . '/mod/wiki/lib.php'); 33 34 /** 35 * Wiki module external functions tests 36 * 37 * @package mod_wiki 38 * @category external 39 * @copyright 2015 Dani Palou <dani@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 * @since Moodle 3.1 42 */ 43 class mod_wiki_external_testcase extends externallib_advanced_testcase { 44 45 /** 46 * Set up for every test 47 */ 48 public function setUp() { 49 global $DB; 50 $this->resetAfterTest(); 51 $this->setAdminUser(); 52 53 // Setup test data. 54 $this->course = $this->getDataGenerator()->create_course(); 55 $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id)); 56 $this->context = context_module::instance($this->wiki->cmid); 57 $this->cm = get_coursemodule_from_instance('wiki', $this->wiki->id); 58 59 // Create users. 60 $this->student = self::getDataGenerator()->create_user(); 61 $this->student2 = self::getDataGenerator()->create_user(); 62 $this->teacher = self::getDataGenerator()->create_user(); 63 64 // Users enrolments. 65 $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); 66 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 67 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); 68 $this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual'); 69 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); 70 71 // Create first pages. 72 $this->firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($this->wiki); 73 } 74 75 /** 76 * Create two collaborative wikis (separate/visible groups), 2 groups and a first page for each wiki and group. 77 */ 78 private function create_collaborative_wikis_with_groups() { 79 // Create groups and add student to one of them. 80 if (!isset($this->group1)) { 81 $this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); 82 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group1->id)); 83 $this->getDataGenerator()->create_group_member(array('userid' => $this->student2->id, 'groupid' => $this->group1->id)); 84 } 85 if (!isset($this->group2)) { 86 $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); 87 } 88 89 // Create two collaborative wikis. 90 $this->wikisep = $this->getDataGenerator()->create_module('wiki', 91 array('course' => $this->course->id, 'groupmode' => SEPARATEGROUPS)); 92 $this->wikivis = $this->getDataGenerator()->create_module('wiki', 93 array('course' => $this->course->id, 'groupmode' => VISIBLEGROUPS)); 94 95 // Create pages. 96 $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); 97 $this->fpsepg1 = $wikigenerator->create_first_page($this->wikisep, array('group' => $this->group1->id)); 98 $this->fpsepg2 = $wikigenerator->create_first_page($this->wikisep, array('group' => $this->group2->id)); 99 $this->fpsepall = $wikigenerator->create_first_page($this->wikisep, array('group' => 0)); // All participants. 100 $this->fpvisg1 = $wikigenerator->create_first_page($this->wikivis, array('group' => $this->group1->id)); 101 $this->fpvisg2 = $wikigenerator->create_first_page($this->wikivis, array('group' => $this->group2->id)); 102 $this->fpvisall = $wikigenerator->create_first_page($this->wikivis, array('group' => 0)); // All participants. 103 } 104 105 /** 106 * Create two individual wikis (separate/visible groups), 2 groups and a first page for each wiki and group. 107 */ 108 private function create_individual_wikis_with_groups() { 109 // Create groups and add student to one of them. 110 if (!isset($this->group1)) { 111 $this->group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); 112 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group1->id)); 113 $this->getDataGenerator()->create_group_member(array('userid' => $this->student2->id, 'groupid' => $this->group1->id)); 114 } 115 if (!isset($this->group2)) { 116 $this->group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id)); 117 } 118 119 // Create two individual wikis. 120 $this->wikisepind = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, 121 'groupmode' => SEPARATEGROUPS, 'wikimode' => 'individual')); 122 $this->wikivisind = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, 123 'groupmode' => VISIBLEGROUPS, 'wikimode' => 'individual')); 124 125 // Create pages. Student can only create pages in his groups. 126 $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); 127 $this->setUser($this->teacher); 128 $this->fpsepg1indt = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); 129 $this->fpsepg2indt = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group2->id)); 130 $this->fpsepallindt = $wikigenerator->create_first_page($this->wikisepind, array('group' => 0)); // All participants. 131 $this->fpvisg1indt = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); 132 $this->fpvisg2indt = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group2->id)); 133 $this->fpvisallindt = $wikigenerator->create_first_page($this->wikivisind, array('group' => 0)); // All participants. 134 135 $this->setUser($this->student); 136 $this->fpsepg1indstu = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); 137 $this->fpvisg1indstu = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); 138 139 $this->setUser($this->student2); 140 $this->fpsepg1indstu2 = $wikigenerator->create_first_page($this->wikisepind, array('group' => $this->group1->id)); 141 $this->fpvisg1indstu2 = $wikigenerator->create_first_page($this->wikivisind, array('group' => $this->group1->id)); 142 143 } 144 145 /* 146 * Test get wikis by courses 147 */ 148 public function test_mod_wiki_get_wikis_by_courses() { 149 150 // Create additional course. 151 $course2 = self::getDataGenerator()->create_course(); 152 153 // Second wiki. 154 $record = new stdClass(); 155 $record->course = $course2->id; 156 $wiki2 = self::getDataGenerator()->create_module('wiki', $record); 157 158 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. 159 $enrol = enrol_get_plugin('manual'); 160 $enrolinstances = enrol_get_instances($course2->id, true); 161 foreach ($enrolinstances as $courseenrolinstance) { 162 if ($courseenrolinstance->enrol == "manual") { 163 $instance2 = $courseenrolinstance; 164 break; 165 } 166 } 167 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id); 168 169 self::setUser($this->student); 170 171 $returndescription = mod_wiki_external::get_wikis_by_courses_returns(); 172 173 // Create what we expect to be returned when querying the two courses. 174 // First for the student user. 175 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'firstpagetitle', 176 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 177 'groupmode', 'groupingid'); 178 179 // Add expected coursemodule and data. 180 $wiki1 = $this->wiki; 181 $wiki1->coursemodule = $wiki1->cmid; 182 $wiki1->introformat = 1; 183 $wiki1->section = 0; 184 $wiki1->visible = true; 185 $wiki1->groupmode = 0; 186 $wiki1->groupingid = 0; 187 $wiki1->introfiles = []; 188 189 $wiki2->coursemodule = $wiki2->cmid; 190 $wiki2->introformat = 1; 191 $wiki2->section = 0; 192 $wiki2->visible = true; 193 $wiki2->groupmode = 0; 194 $wiki2->groupingid = 0; 195 $wiki2->introfiles = []; 196 197 foreach ($expectedfields as $field) { 198 $expected1[$field] = $wiki1->{$field}; 199 $expected2[$field] = $wiki2->{$field}; 200 } 201 // Users can create pages by default. 202 $expected1['cancreatepages'] = true; 203 $expected2['cancreatepages'] = true; 204 205 $expectedwikis = array($expected2, $expected1); 206 207 // Call the external function passing course ids. 208 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id)); 209 $result = external_api::clean_returnvalue($returndescription, $result); 210 211 $this->assertEquals($expectedwikis, $result['wikis']); 212 $this->assertCount(0, $result['warnings']); 213 214 // Call the external function without passing course id. 215 $result = mod_wiki_external::get_wikis_by_courses(); 216 $result = external_api::clean_returnvalue($returndescription, $result); 217 $this->assertEquals($expectedwikis, $result['wikis']); 218 $this->assertCount(0, $result['warnings']); 219 220 // Unenrol user from second course and alter expected wikis. 221 $enrol->unenrol_user($instance2, $this->student->id); 222 array_shift($expectedwikis); 223 224 // Call the external function without passing course id. 225 $result = mod_wiki_external::get_wikis_by_courses(); 226 $result = external_api::clean_returnvalue($returndescription, $result); 227 $this->assertEquals($expectedwikis, $result['wikis']); 228 229 // Call for the second course we unenrolled the user from, expected warning. 230 $result = mod_wiki_external::get_wikis_by_courses(array($course2->id)); 231 $this->assertCount(1, $result['warnings']); 232 $this->assertEquals('1', $result['warnings'][0]['warningcode']); 233 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); 234 235 // Now, try as a teacher for getting all the additional fields. 236 self::setUser($this->teacher); 237 238 $additionalfields = array('timecreated', 'timemodified'); 239 240 foreach ($additionalfields as $field) { 241 $expectedwikis[0][$field] = $wiki1->{$field}; 242 } 243 244 $result = mod_wiki_external::get_wikis_by_courses(); 245 $result = external_api::clean_returnvalue($returndescription, $result); 246 $this->assertEquals($expectedwikis, $result['wikis']); 247 248 // Admin also should get all the information. 249 self::setAdminUser(); 250 251 $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); 252 $result = external_api::clean_returnvalue($returndescription, $result); 253 $this->assertEquals($expectedwikis, $result['wikis']); 254 255 // Now, prohibit capabilities. 256 $this->setUser($this->student); 257 $contextcourse1 = context_course::instance($this->course->id); 258 // Prohibit capability = mod:wiki:viewpage on Course1 for students. 259 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); 260 accesslib_clear_all_caches_for_unit_testing(); 261 262 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); 263 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); 264 $this->assertFalse(isset($wikis['wikis'][0]['intro'])); 265 266 // Prohibit capability = mod:wiki:createpage on Course1 for students. 267 assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); 268 accesslib_clear_all_caches_for_unit_testing(); 269 270 $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); 271 $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); 272 $this->assertFalse($wikis['wikis'][0]['cancreatepages']); 273 274 } 275 276 /** 277 * Test view_wiki. 278 */ 279 public function test_view_wiki() { 280 281 // Test invalid instance id. 282 try { 283 mod_wiki_external::view_wiki(0); 284 $this->fail('Exception expected due to invalid mod_wiki instance id.'); 285 } catch (moodle_exception $e) { 286 $this->assertEquals('incorrectwikiid', $e->errorcode); 287 } 288 289 // Test not-enrolled user. 290 $usernotenrolled = self::getDataGenerator()->create_user(); 291 $this->setUser($usernotenrolled); 292 try { 293 mod_wiki_external::view_wiki($this->wiki->id); 294 $this->fail('Exception expected due to not enrolled user.'); 295 } catch (moodle_exception $e) { 296 $this->assertEquals('requireloginerror', $e->errorcode); 297 } 298 299 // Test user with full capabilities. 300 $this->setUser($this->student); 301 302 // Trigger and capture the event. 303 $sink = $this->redirectEvents(); 304 305 $result = mod_wiki_external::view_wiki($this->wiki->id); 306 $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result); 307 308 $events = $sink->get_events(); 309 $this->assertCount(1, $events); 310 $event = array_shift($events); 311 312 // Checking that the event contains the expected values. 313 $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event); 314 $this->assertEquals($this->context, $event->get_context()); 315 $moodlewiki = new \moodle_url('/mod/wiki/view.php', array('id' => $this->cm->id)); 316 $this->assertEquals($moodlewiki, $event->get_url()); 317 $this->assertEventContextNotUsed($event); 318 $this->assertNotEmpty($event->get_name()); 319 320 // Test user with no capabilities. 321 // We need a explicit prohibit since this capability is allowed for students by default. 322 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); 323 accesslib_clear_all_caches_for_unit_testing(); 324 325 try { 326 mod_wiki_external::view_wiki($this->wiki->id); 327 $this->fail('Exception expected due to missing capability.'); 328 } catch (moodle_exception $e) { 329 $this->assertEquals('cannotviewpage', $e->errorcode); 330 } 331 332 } 333 334 /** 335 * Test view_page. 336 */ 337 public function test_view_page() { 338 339 // Test invalid page id. 340 try { 341 mod_wiki_external::view_page(0); 342 $this->fail('Exception expected due to invalid view_page page id.'); 343 } catch (moodle_exception $e) { 344 $this->assertEquals('incorrectpageid', $e->errorcode); 345 } 346 347 // Test not-enrolled user. 348 $usernotenrolled = self::getDataGenerator()->create_user(); 349 $this->setUser($usernotenrolled); 350 try { 351 mod_wiki_external::view_page($this->firstpage->id); 352 $this->fail('Exception expected due to not enrolled user.'); 353 } catch (moodle_exception $e) { 354 $this->assertEquals('requireloginerror', $e->errorcode); 355 } 356 357 // Test user with full capabilities. 358 $this->setUser($this->student); 359 360 // Trigger and capture the event. 361 $sink = $this->redirectEvents(); 362 363 $result = mod_wiki_external::view_page($this->firstpage->id); 364 $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result); 365 366 $events = $sink->get_events(); 367 $this->assertCount(1, $events); 368 $event = array_shift($events); 369 370 // Checking that the event contains the expected values. 371 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event); 372 $this->assertEquals($this->context, $event->get_context()); 373 $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $this->firstpage->id)); 374 $this->assertEquals($pageurl, $event->get_url()); 375 $this->assertEventContextNotUsed($event); 376 $this->assertNotEmpty($event->get_name()); 377 378 // Test user with no capabilities. 379 // We need a explicit prohibit since this capability is allowed for students by default. 380 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); 381 accesslib_clear_all_caches_for_unit_testing(); 382 383 try { 384 mod_wiki_external::view_page($this->firstpage->id); 385 $this->fail('Exception expected due to missing capability.'); 386 } catch (moodle_exception $e) { 387 $this->assertEquals('cannotviewpage', $e->errorcode); 388 } 389 390 } 391 392 /** 393 * Test get_subwikis. 394 */ 395 public function test_get_subwikis() { 396 397 // Test invalid wiki id. 398 try { 399 mod_wiki_external::get_subwikis(0); 400 $this->fail('Exception expected due to invalid get_subwikis wiki id.'); 401 } catch (moodle_exception $e) { 402 $this->assertEquals('incorrectwikiid', $e->errorcode); 403 } 404 405 // Test not-enrolled user. 406 $usernotenrolled = self::getDataGenerator()->create_user(); 407 $this->setUser($usernotenrolled); 408 try { 409 mod_wiki_external::get_subwikis($this->wiki->id); 410 $this->fail('Exception expected due to not enrolled user.'); 411 } catch (moodle_exception $e) { 412 $this->assertEquals('requireloginerror', $e->errorcode); 413 } 414 415 // Test user with full capabilities. 416 $this->setUser($this->student); 417 418 // Create what we expect to be returned. We only test a basic case because deep testing is already done 419 // in the tests for wiki_get_visible_subwikis. 420 $expectedsubwikis = array(); 421 $expectedsubwiki = array( 422 'id' => $this->firstpage->subwikiid, 423 'wikiid' => $this->wiki->id, 424 'groupid' => 0, 425 'userid' => 0, 426 'canedit' => true 427 ); 428 $expectedsubwikis[] = $expectedsubwiki; 429 430 $result = mod_wiki_external::get_subwikis($this->wiki->id); 431 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result); 432 $this->assertEquals($expectedsubwikis, $result['subwikis']); 433 $this->assertCount(0, $result['warnings']); 434 435 // Test user with no capabilities. 436 // We need a explicit prohibit since this capability is allowed for students by default. 437 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); 438 accesslib_clear_all_caches_for_unit_testing(); 439 440 try { 441 mod_wiki_external::get_subwikis($this->wiki->id); 442 $this->fail('Exception expected due to missing capability.'); 443 } catch (moodle_exception $e) { 444 $this->assertEquals('nopermissions', $e->errorcode); 445 } 446 447 } 448 449 /** 450 * Test get_subwiki_pages using an invalid wiki instance. 451 * 452 * @expectedException moodle_exception 453 */ 454 public function test_get_subwiki_pages_invalid_instance() { 455 mod_wiki_external::get_subwiki_pages(0); 456 } 457 458 /** 459 * Test get_subwiki_pages using a user not enrolled in the course. 460 * 461 * @expectedException require_login_exception 462 */ 463 public function test_get_subwiki_pages_unenrolled_user() { 464 // Create and use the user. 465 $usernotenrolled = self::getDataGenerator()->create_user(); 466 $this->setUser($usernotenrolled); 467 468 mod_wiki_external::get_subwiki_pages($this->wiki->id); 469 } 470 471 /** 472 * Test get_subwiki_pages using a hidden wiki as student. 473 * 474 * @expectedException require_login_exception 475 */ 476 public function test_get_subwiki_pages_hidden_wiki_as_student() { 477 // Create a hidden wiki and try to get the list of pages. 478 $hiddenwiki = $this->getDataGenerator()->create_module('wiki', 479 array('course' => $this->course->id, 'visible' => false)); 480 481 $this->setUser($this->student); 482 mod_wiki_external::get_subwiki_pages($hiddenwiki->id); 483 } 484 485 /** 486 * Test get_subwiki_pages without the viewpage capability. 487 * 488 * @expectedException moodle_exception 489 */ 490 public function test_get_subwiki_pages_without_viewpage_capability() { 491 // Prohibit capability = mod/wiki:viewpage on the course for students. 492 $contextcourse = context_course::instance($this->course->id); 493 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); 494 accesslib_clear_all_caches_for_unit_testing(); 495 496 $this->setUser($this->student); 497 mod_wiki_external::get_subwiki_pages($this->wiki->id); 498 } 499 500 /** 501 * Test get_subwiki_pages using an invalid userid. 502 * 503 * @expectedException moodle_exception 504 */ 505 public function test_get_subwiki_pages_invalid_userid() { 506 // Create an individual wiki. 507 $indwiki = $this->getDataGenerator()->create_module('wiki', 508 array('course' => $this->course->id, 'wikimode' => 'individual')); 509 510 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10); 511 } 512 513 /** 514 * Test get_subwiki_pages using an invalid groupid. 515 * 516 * @expectedException moodle_exception 517 */ 518 public function test_get_subwiki_pages_invalid_groupid() { 519 // Create testing data. 520 $this->create_collaborative_wikis_with_groups(); 521 522 mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111); 523 } 524 525 /** 526 * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups. 527 * 528 * @expectedException moodle_exception 529 */ 530 public function test_get_subwiki_pages_individual_student_see_other_user() { 531 // Create an individual wiki. 532 $indwiki = $this->getDataGenerator()->create_module('wiki', 533 array('course' => $this->course->id, 'wikimode' => 'individual')); 534 535 $this->setUser($this->student); 536 mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); 537 } 538 539 /** 540 * Test get_subwiki_pages, check that a student can't get the pages from another group in 541 * a collaborative wiki using separate groups. 542 * 543 * @expectedException moodle_exception 544 */ 545 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_other_group() { 546 // Create testing data. 547 $this->create_collaborative_wikis_with_groups(); 548 549 $this->setUser($this->student); 550 mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id); 551 } 552 553 /** 554 * Test get_subwiki_pages, check that a student can't get the pages from another group in 555 * an individual wiki using separate groups. 556 * 557 * @expectedException moodle_exception 558 */ 559 public function test_get_subwiki_pages_individual_separate_groups_student_see_other_group() { 560 // Create testing data. 561 $this->create_individual_wikis_with_groups(); 562 563 $this->setUser($this->student); 564 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id); 565 } 566 567 /** 568 * Test get_subwiki_pages, check that a student can't get the pages from all participants in 569 * a collaborative wiki using separate groups. 570 * 571 * @expectedException moodle_exception 572 */ 573 public function test_get_subwiki_pages_collaborative_separate_groups_student_see_all_participants() { 574 // Create testing data. 575 $this->create_collaborative_wikis_with_groups(); 576 577 $this->setUser($this->student); 578 mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); 579 } 580 581 /** 582 * Test get_subwiki_pages, check that a student can't get the pages from all participants in 583 * an individual wiki using separate groups. 584 * 585 * @expectedException moodle_exception 586 */ 587 public function test_get_subwiki_pages_individual_separate_groups_student_see_all_participants() { 588 // Create testing data. 589 $this->create_individual_wikis_with_groups(); 590 591 $this->setUser($this->student); 592 mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id); 593 } 594 595 /** 596 * Test get_subwiki_pages without groups and collaborative wiki. 597 */ 598 public function test_get_subwiki_pages_collaborative() { 599 600 // Test user with full capabilities. 601 $this->setUser($this->student); 602 603 // Set expected result: first page. 604 $expectedpages = array(); 605 $expectedfirstpage = (array) $this->firstpage; 606 $expectedfirstpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability. 607 $expectedfirstpage['firstpage'] = true; 608 $expectedfirstpage['contentformat'] = 1; 609 $expectedpages[] = $expectedfirstpage; 610 611 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); 612 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 613 $this->assertEquals($expectedpages, $result['pages']); 614 615 // Check that groupid param is ignored since the wiki isn't using groups. 616 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234); 617 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 618 $this->assertEquals($expectedpages, $result['pages']); 619 620 // Check that userid param is ignored since the wiki is collaborative. 621 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234, 1234); 622 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 623 $this->assertEquals($expectedpages, $result['pages']); 624 625 // Add a new page to the wiki and test again. We'll use a custom title so it's returned first if sorted by title. 626 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page( 627 $this->wiki, array('title' => 'AAA')); 628 629 $expectednewpage = (array) $newpage; 630 $expectednewpage['caneditpage'] = true; // No groups and students have 'mod/wiki:editpage' capability. 631 $expectednewpage['firstpage'] = false; 632 $expectednewpage['contentformat'] = 1; 633 array_unshift($expectedpages, $expectednewpage); // Add page to the beginning since it orders by title by default. 634 635 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); 636 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 637 $this->assertEquals($expectedpages, $result['pages']); 638 639 // Now we'll order by ID. Since first page was created first it'll have a lower ID. 640 $expectedpages = array($expectedfirstpage, $expectednewpage); 641 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id')); 642 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 643 $this->assertEquals($expectedpages, $result['pages']); 644 645 // Check that WS doesn't return page content if includecontent is false, it returns the size instead. 646 foreach ($expectedpages as $i => $expectedpage) { 647 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { 648 $expectedpages[$i]['contentsize'] = mb_strlen($expectedpages[$i]['cachedcontent'], '8bit'); 649 } else { 650 $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']); 651 } 652 unset($expectedpages[$i]['cachedcontent']); 653 unset($expectedpages[$i]['contentformat']); 654 } 655 $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0)); 656 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 657 $this->assertEquals($expectedpages, $result['pages']); 658 } 659 660 /** 661 * Test get_subwiki_pages without groups. 662 */ 663 public function test_get_subwiki_pages_individual() { 664 665 // Create an individual wiki to test userid param. 666 $indwiki = $this->getDataGenerator()->create_module('wiki', 667 array('course' => $this->course->id, 'wikimode' => 'individual')); 668 669 // Perform a request before creating any page to check that an empty array is returned if subwiki doesn't exist. 670 $result = mod_wiki_external::get_subwiki_pages($indwiki->id); 671 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 672 $this->assertEquals(array(), $result['pages']); 673 674 // Create first pages as student and teacher. 675 $this->setUser($this->student); 676 $indfirstpagestudent = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki); 677 $this->setUser($this->teacher); 678 $indfirstpageteacher = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($indwiki); 679 680 // Check that teacher can get his pages. 681 $expectedteacherpage = (array) $indfirstpageteacher; 682 $expectedteacherpage['caneditpage'] = true; 683 $expectedteacherpage['firstpage'] = true; 684 $expectedteacherpage['contentformat'] = 1; 685 $expectedpages = array($expectedteacherpage); 686 687 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); 688 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 689 $this->assertEquals($expectedpages, $result['pages']); 690 691 // Check that the teacher can see the student's pages. 692 $expectedstudentpage = (array) $indfirstpagestudent; 693 $expectedstudentpage['caneditpage'] = true; 694 $expectedstudentpage['firstpage'] = true; 695 $expectedstudentpage['contentformat'] = 1; 696 $expectedpages = array($expectedstudentpage); 697 698 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); 699 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 700 $this->assertEquals($expectedpages, $result['pages']); 701 702 // Now check that student can get his pages. 703 $this->setUser($this->student); 704 705 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); 706 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 707 $this->assertEquals($expectedpages, $result['pages']); 708 709 // Check that not using userid uses current user. 710 $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0); 711 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 712 $this->assertEquals($expectedpages, $result['pages']); 713 } 714 715 /** 716 * Test get_subwiki_pages with groups and collaborative wikis. 717 */ 718 public function test_get_subwiki_pages_separate_groups_collaborative() { 719 720 // Create testing data. 721 $this->create_collaborative_wikis_with_groups(); 722 723 $this->setUser($this->student); 724 725 // Try to get pages from a valid group in separate groups wiki. 726 727 $expectedpage = (array) $this->fpsepg1; 728 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability. 729 $expectedpage['firstpage'] = true; 730 $expectedpage['contentformat'] = 1; 731 $expectedpages = array($expectedpage); 732 733 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); 734 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 735 $this->assertEquals($expectedpages, $result['pages']); 736 737 // Let's check that not using groupid returns the same result (current group). 738 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id); 739 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 740 $this->assertEquals($expectedpages, $result['pages']); 741 742 // Check that teacher can view a group pages without belonging to it. 743 $this->setUser($this->teacher); 744 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); 745 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 746 $this->assertEquals($expectedpages, $result['pages']); 747 748 // Check that teacher can get the pages from all participants. 749 $expectedpage = (array) $this->fpsepall; 750 $expectedpage['caneditpage'] = true; 751 $expectedpage['firstpage'] = true; 752 $expectedpage['contentformat'] = 1; 753 $expectedpages = array($expectedpage); 754 755 $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); 756 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 757 $this->assertEquals($expectedpages, $result['pages']); 758 } 759 760 /** 761 * Test get_subwiki_pages with groups and collaborative wikis. 762 */ 763 public function test_get_subwiki_pages_visible_groups_collaborative() { 764 765 // Create testing data. 766 $this->create_collaborative_wikis_with_groups(); 767 768 $this->setUser($this->student); 769 770 // Try to get pages from a valid group in visible groups wiki. 771 772 $expectedpage = (array) $this->fpvisg1; 773 $expectedpage['caneditpage'] = true; // User belongs to group and has 'mod/wiki:editpage' capability. 774 $expectedpage['firstpage'] = true; 775 $expectedpage['contentformat'] = 1; 776 $expectedpages = array($expectedpage); 777 778 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group1->id); 779 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 780 $this->assertEquals($expectedpages, $result['pages']); 781 782 // Check that with visible groups a student can get the pages of groups he doesn't belong to. 783 $expectedpage = (array) $this->fpvisg2; 784 $expectedpage['caneditpage'] = false; // User doesn't belong to group so he can't edit the page. 785 $expectedpage['firstpage'] = true; 786 $expectedpage['contentformat'] = 1; 787 $expectedpages = array($expectedpage); 788 789 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group2->id); 790 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 791 $this->assertEquals($expectedpages, $result['pages']); 792 793 // Check that with visible groups a student can get the pages of all participants. 794 $expectedpage = (array) $this->fpvisall; 795 $expectedpage['caneditpage'] = false; 796 $expectedpage['firstpage'] = true; 797 $expectedpage['contentformat'] = 1; 798 $expectedpages = array($expectedpage); 799 800 $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, 0); 801 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 802 $this->assertEquals($expectedpages, $result['pages']); 803 } 804 805 /** 806 * Test get_subwiki_pages with groups and individual wikis. 807 */ 808 public function test_get_subwiki_pages_separate_groups_individual() { 809 810 // Create testing data. 811 $this->create_individual_wikis_with_groups(); 812 813 $this->setUser($this->student); 814 815 // Check that student can retrieve his pages from separate wiki. 816 $expectedpage = (array) $this->fpsepg1indstu; 817 $expectedpage['caneditpage'] = true; 818 $expectedpage['firstpage'] = true; 819 $expectedpage['contentformat'] = 1; 820 $expectedpages = array($expectedpage); 821 822 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); 823 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 824 $this->assertEquals($expectedpages, $result['pages']); 825 826 // Check that not using userid uses current user. 827 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id); 828 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 829 $this->assertEquals($expectedpages, $result['pages']); 830 831 // Check that the teacher can see the student pages. 832 $this->setUser($this->teacher); 833 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); 834 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 835 $this->assertEquals($expectedpages, $result['pages']); 836 837 // Check that a student can see pages from another user that belongs to his groups. 838 $this->setUser($this->student); 839 $expectedpage = (array) $this->fpsepg1indstu2; 840 $expectedpage['caneditpage'] = false; 841 $expectedpage['firstpage'] = true; 842 $expectedpage['contentformat'] = 1; 843 $expectedpages = array($expectedpage); 844 845 $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student2->id); 846 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 847 $this->assertEquals($expectedpages, $result['pages']); 848 } 849 850 /** 851 * Test get_subwiki_pages with groups and individual wikis. 852 */ 853 public function test_get_subwiki_pages_visible_groups_individual() { 854 855 // Create testing data. 856 $this->create_individual_wikis_with_groups(); 857 858 $this->setUser($this->student); 859 860 // Check that student can retrieve his pages from visible wiki. 861 $expectedpage = (array) $this->fpvisg1indstu; 862 $expectedpage['caneditpage'] = true; 863 $expectedpage['firstpage'] = true; 864 $expectedpage['contentformat'] = 1; 865 $expectedpages = array($expectedpage); 866 867 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group1->id, $this->student->id); 868 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 869 $this->assertEquals($expectedpages, $result['pages']); 870 871 // Check that student can see teacher pages in visible groups, even if the user doesn't belong to the group. 872 $expectedpage = (array) $this->fpvisg2indt; 873 $expectedpage['caneditpage'] = false; 874 $expectedpage['firstpage'] = true; 875 $expectedpage['contentformat'] = 1; 876 $expectedpages = array($expectedpage); 877 878 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group2->id, $this->teacher->id); 879 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 880 $this->assertEquals($expectedpages, $result['pages']); 881 882 // Check that with visible groups a student can get the pages of all participants. 883 $expectedpage = (array) $this->fpvisallindt; 884 $expectedpage['caneditpage'] = false; 885 $expectedpage['firstpage'] = true; 886 $expectedpage['contentformat'] = 1; 887 $expectedpages = array($expectedpage); 888 889 $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, 0, $this->teacher->id); 890 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); 891 $this->assertEquals($expectedpages, $result['pages']); 892 } 893 894 /** 895 * Test get_page_contents using an invalid pageid. 896 * 897 * @expectedException moodle_exception 898 */ 899 public function test_get_page_contents_invalid_pageid() { 900 mod_wiki_external::get_page_contents(0); 901 } 902 903 /** 904 * Test get_page_contents using a user not enrolled in the course. 905 * 906 * @expectedException require_login_exception 907 */ 908 public function test_get_page_contents_unenrolled_user() { 909 // Create and use the user. 910 $usernotenrolled = self::getDataGenerator()->create_user(); 911 $this->setUser($usernotenrolled); 912 913 mod_wiki_external::get_page_contents($this->firstpage->id); 914 } 915 916 /** 917 * Test get_page_contents using a hidden wiki as student. 918 * 919 * @expectedException require_login_exception 920 */ 921 public function test_get_page_contents_hidden_wiki_as_student() { 922 // Create a hidden wiki and try to get a page contents. 923 $hiddenwiki = $this->getDataGenerator()->create_module('wiki', 924 array('course' => $this->course->id, 'visible' => false)); 925 $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki); 926 927 $this->setUser($this->student); 928 mod_wiki_external::get_page_contents($hiddenpage->id); 929 } 930 931 /** 932 * Test get_page_contents without the viewpage capability. 933 * 934 * @expectedException moodle_exception 935 */ 936 public function test_get_page_contents_without_viewpage_capability() { 937 // Prohibit capability = mod/wiki:viewpage on the course for students. 938 $contextcourse = context_course::instance($this->course->id); 939 assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); 940 accesslib_clear_all_caches_for_unit_testing(); 941 942 $this->setUser($this->student); 943 mod_wiki_external::get_page_contents($this->firstpage->id); 944 } 945 946 /** 947 * Test get_page_contents, check that a student can't get a page from another group when 948 * using separate groups. 949 * 950 * @expectedException moodle_exception 951 */ 952 public function test_get_page_contents_separate_groups_student_see_other_group() { 953 // Create testing data. 954 $this->create_individual_wikis_with_groups(); 955 956 $this->setUser($this->student); 957 mod_wiki_external::get_page_contents($this->fpsepg2indt->id); 958 } 959 960 /** 961 * Test get_page_contents without groups. We won't test all the possible cases because that's already 962 * done in the tests for get_subwiki_pages. 963 */ 964 public function test_get_page_contents() { 965 966 // Test user with full capabilities. 967 $this->setUser($this->student); 968 969 // Set expected result: first page. 970 $expectedpage = array( 971 'id' => $this->firstpage->id, 972 'wikiid' => $this->wiki->id, 973 'subwikiid' => $this->firstpage->subwikiid, 974 'groupid' => 0, // No groups. 975 'userid' => 0, // Collaborative. 976 'title' => $this->firstpage->title, 977 'cachedcontent' => $this->firstpage->cachedcontent, 978 'contentformat' => 1, 979 'caneditpage' => true 980 ); 981 982 $result = mod_wiki_external::get_page_contents($this->firstpage->id); 983 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); 984 $this->assertEquals($expectedpage, $result['page']); 985 986 // Add a new page to the wiki and test with it. 987 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wiki); 988 989 $expectedpage['id'] = $newpage->id; 990 $expectedpage['title'] = $newpage->title; 991 $expectedpage['cachedcontent'] = $newpage->cachedcontent; 992 993 $result = mod_wiki_external::get_page_contents($newpage->id); 994 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); 995 $this->assertEquals($expectedpage, $result['page']); 996 } 997 998 /** 999 * Test get_page_contents with groups. We won't test all the possible cases because that's already 1000 * done in the tests for get_subwiki_pages. 1001 */ 1002 public function test_get_page_contents_with_groups() { 1003 1004 // Create testing data. 1005 $this->create_individual_wikis_with_groups(); 1006 1007 // Try to get page from a valid group in separate groups wiki. 1008 $this->setUser($this->student); 1009 1010 $expectedfpsepg1indstu = array( 1011 'id' => $this->fpsepg1indstu->id, 1012 'wikiid' => $this->wikisepind->id, 1013 'subwikiid' => $this->fpsepg1indstu->subwikiid, 1014 'groupid' => $this->group1->id, 1015 'userid' => $this->student->id, 1016 'title' => $this->fpsepg1indstu->title, 1017 'cachedcontent' => $this->fpsepg1indstu->cachedcontent, 1018 'contentformat' => 1, 1019 'caneditpage' => true 1020 ); 1021 1022 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); 1023 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); 1024 $this->assertEquals($expectedfpsepg1indstu, $result['page']); 1025 1026 // Check that teacher can view a group pages without belonging to it. 1027 $this->setUser($this->teacher); 1028 $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); 1029 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); 1030 $this->assertEquals($expectedfpsepg1indstu, $result['page']); 1031 } 1032 1033 /** 1034 * Test get_subwiki_files using a wiki without files. 1035 */ 1036 public function test_get_subwiki_files_no_files() { 1037 $result = mod_wiki_external::get_subwiki_files($this->wiki->id); 1038 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); 1039 $this->assertCount(0, $result['files']); 1040 $this->assertCount(0, $result['warnings']); 1041 } 1042 1043 /** 1044 * Test get_subwiki_files, check that a student can't get files from another group's subwiki when 1045 * using separate groups. 1046 * 1047 * @expectedException moodle_exception 1048 */ 1049 public function test_get_subwiki_files_separate_groups_student_see_other_group() { 1050 // Create testing data. 1051 $this->create_collaborative_wikis_with_groups(); 1052 1053 $this->setUser($this->student); 1054 mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id); 1055 } 1056 1057 /** 1058 * Test get_subwiki_files using a collaborative wiki without groups. 1059 */ 1060 public function test_get_subwiki_files_collaborative_no_groups() { 1061 $this->setUser($this->student); 1062 1063 // Add a file as subwiki attachment. 1064 $fs = get_file_storage(); 1065 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments', 1066 'contextid' => $this->context->id, 'itemid' => $this->firstpage->subwikiid, 1067 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time()); 1068 $content = 'IMAGE'; 1069 $fs->create_file_from_string($file, $content); 1070 1071 $expectedfile = array( 1072 'filename' => $file['filename'], 1073 'filepath' => $file['filepath'], 1074 'mimetype' => 'image/jpeg', 1075 'filesize' => strlen($content), 1076 'timemodified' => $file['timemodified'], 1077 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], 1078 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), 1079 ); 1080 1081 // Call the WS and check that it returns this file. 1082 $result = mod_wiki_external::get_subwiki_files($this->wiki->id); 1083 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); 1084 $this->assertCount(1, $result['files']); 1085 $this->assertEquals($expectedfile, $result['files'][0]); 1086 1087 // Now add another file to the same subwiki. 1088 $file['filename'] = 'Another image.jpg'; 1089 $file['timemodified'] = time(); 1090 $content = 'ANOTHER IMAGE'; 1091 $fs->create_file_from_string($file, $content); 1092 1093 $expectedfile['filename'] = $file['filename']; 1094 $expectedfile['timemodified'] = $file['timemodified']; 1095 $expectedfile['filesize'] = strlen($content); 1096 $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], 1097 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']); 1098 1099 // Call the WS and check that it returns both files file. 1100 $result = mod_wiki_external::get_subwiki_files($this->wiki->id); 1101 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); 1102 $this->assertCount(2, $result['files']); 1103 // The new file is returned first because they're returned in alphabetical order. 1104 $this->assertEquals($expectedfile, $result['files'][0]); 1105 } 1106 1107 /** 1108 * Test get_subwiki_files using an individual wiki with visible groups. 1109 */ 1110 public function test_get_subwiki_files_visible_groups_individual() { 1111 // Create testing data. 1112 $this->create_individual_wikis_with_groups(); 1113 1114 $this->setUser($this->student); 1115 1116 // Add a file as subwiki attachment in the student group 1 subwiki. 1117 $fs = get_file_storage(); 1118 $contextwiki = context_module::instance($this->wikivisind->cmid); 1119 $file = array('component' => 'mod_wiki', 'filearea' => 'attachments', 1120 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid, 1121 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time()); 1122 $content = 'IMAGE'; 1123 $fs->create_file_from_string($file, $content); 1124 1125 $expectedfile = array( 1126 'filename' => $file['filename'], 1127 'filepath' => $file['filepath'], 1128 'mimetype' => 'image/jpeg', 1129 'filesize' => strlen($content), 1130 'timemodified' => $file['timemodified'], 1131 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], 1132 $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), 1133 ); 1134 1135 // Call the WS and check that it returns this file. 1136 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id); 1137 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); 1138 $this->assertCount(1, $result['files']); 1139 $this->assertEquals($expectedfile, $result['files'][0]); 1140 1141 // Now check that a teacher can see it too. 1142 $this->setUser($this->teacher); 1143 $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id); 1144 $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); 1145 $this->assertCount(1, $result['files']); 1146 $this->assertEquals($expectedfile, $result['files'][0]); 1147 } 1148 1149 1150 /** 1151 * Test get_page_for_editing. We won't test all the possible cases because that's already 1152 * done in the tests for wiki_parser_proxy::get_section. 1153 */ 1154 public function test_get_page_for_editing() { 1155 1156 $this->create_individual_wikis_with_groups(); 1157 1158 // We add a <span> in the first title to verify the WS works sending HTML in section. 1159 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section'; 1160 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section'; 1161 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page( 1162 $this->wiki, array('content' => $pagecontent)); 1163 1164 // Test user with full capabilities. 1165 $this->setUser($this->student); 1166 1167 // Set expected result: Full Page content. 1168 $expected = array( 1169 'content' => $pagecontent, 1170 'contentformat' => 'html', 1171 'version' => '1' 1172 ); 1173 1174 $result = mod_wiki_external::get_page_for_editing($newpage->id); 1175 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); 1176 $this->assertEquals($expected, $result['pagesection']); 1177 1178 // Set expected result: Section Page content. 1179 $expected = array( 1180 'content' => $sectioncontent, 1181 'contentformat' => 'html', 1182 'version' => '1' 1183 ); 1184 1185 $result = mod_wiki_external::get_page_for_editing($newpage->id, '<span>Title1</span>'); 1186 $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); 1187 $this->assertEquals($expected, $result['pagesection']); 1188 } 1189 1190 /** 1191 * Test new_page. We won't test all the possible cases because that's already 1192 * done in the tests for wiki_create_page. 1193 */ 1194 public function test_new_page() { 1195 1196 $this->create_individual_wikis_with_groups(); 1197 1198 $sectioncontent = '<h1>Title1</h1>Text inside section'; 1199 $pagecontent = $sectioncontent.'<h1>Title2</h1>Text inside section'; 1200 $pagetitle = 'Page Title'; 1201 1202 // Test user with full capabilities. 1203 $this->setUser($this->student); 1204 1205 // Test on existing subwiki. 1206 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); 1207 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); 1208 $this->assertInternalType('int', $result['pageid']); 1209 1210 $version = wiki_get_current_version($result['pageid']); 1211 $this->assertEquals($pagecontent, $version->content); 1212 $this->assertEquals('html', $version->contentformat); 1213 1214 $page = wiki_get_page($result['pageid']); 1215 $this->assertEquals($pagetitle, $page->title); 1216 1217 // Test existing page creation. 1218 try { 1219 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); 1220 $this->fail('Exception expected due to creation of an existing page.'); 1221 } catch (moodle_exception $e) { 1222 $this->assertEquals('pageexists', $e->errorcode); 1223 } 1224 1225 // Test on non existing subwiki. Add student to group2 to have a new subwiki to be created. 1226 $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id)); 1227 $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id, 1228 $this->group2->id); 1229 $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); 1230 $this->assertInternalType('int', $result['pageid']); 1231 1232 $version = wiki_get_current_version($result['pageid']); 1233 $this->assertEquals($pagecontent, $version->content); 1234 $this->assertEquals('html', $version->contentformat); 1235 1236 $page = wiki_get_page($result['pageid']); 1237 $this->assertEquals($pagetitle, $page->title); 1238 1239 $subwiki = wiki_get_subwiki($page->subwikiid); 1240 $expected = new StdClass(); 1241 $expected->id = $subwiki->id; 1242 $expected->wikiid = $this->wikisepind->id; 1243 $expected->groupid = $this->group2->id; 1244 $expected->userid = $this->student->id; 1245 $this->assertEquals($expected, $subwiki); 1246 1247 // Check page creation for a user not in course. 1248 $this->studentnotincourse = self::getDataGenerator()->create_user(); 1249 $this->anothercourse = $this->getDataGenerator()->create_course(); 1250 $this->groupnotincourse = $this->getDataGenerator()->create_group(array('courseid' => $this->anothercourse->id)); 1251 1252 try { 1253 mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, 1254 $this->studentnotincourse->id, $this->groupnotincourse->id); 1255 $this->fail('Exception expected due to creation of an invalid subwiki creation.'); 1256 } catch (moodle_exception $e) { 1257 $this->assertEquals('cannoteditpage', $e->errorcode); 1258 } 1259 1260 } 1261 1262 /** 1263 * Test edit_page. We won't test all the possible cases because that's already 1264 * done in the tests for wiki_save_section / wiki_save_page. 1265 */ 1266 public function test_edit_page() { 1267 1268 $this->create_individual_wikis_with_groups(); 1269 1270 // Test user with full capabilities. 1271 $this->setUser($this->student); 1272 1273 $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wikisepind, 1274 array('group' => $this->group1->id, 'content' => 'Test')); 1275 1276 // Test edit whole page. 1277 // We add <span> in the titles to verify the WS works sending HTML in section. 1278 $sectioncontent = '<h1><span>Title1</span></h1>Text inside section'; 1279 $newpagecontent = $sectioncontent.'<h1><span>Title2</span></h1>Text inside section'; 1280 1281 $result = mod_wiki_external::edit_page($newpage->id, $newpagecontent); 1282 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); 1283 $this->assertInternalType('int', $result['pageid']); 1284 1285 $version = wiki_get_current_version($result['pageid']); 1286 $this->assertEquals($newpagecontent, $version->content); 1287 1288 // Test edit section. 1289 $newsectioncontent = '<h1><span>Title2</span></h1>New test2'; 1290 $section = '<span>Title2</span>'; 1291 1292 $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); 1293 $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); 1294 $this->assertInternalType('int', $result['pageid']); 1295 1296 $expected = $sectioncontent . $newsectioncontent; 1297 1298 $version = wiki_get_current_version($result['pageid']); 1299 $this->assertEquals($expected, $version->content); 1300 1301 // Test locked section. 1302 $newsectioncontent = '<h1><span>Title2</span></h1>New test2'; 1303 $section = '<span>Title2</span>'; 1304 1305 try { 1306 // Using user 1 to avoid other users to edit. 1307 wiki_set_lock($newpage->id, 1, $section, true); 1308 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); 1309 $this->fail('Exception expected due to locked section'); 1310 } catch (moodle_exception $e) { 1311 $this->assertEquals('pageislocked', $e->errorcode); 1312 } 1313 1314 // Test edit non existing section. 1315 $newsectioncontent = '<h1>Title3</h1>New test3'; 1316 $section = 'Title3'; 1317 1318 try { 1319 mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); 1320 $this->fail('Exception expected due to non existing section in the page.'); 1321 } catch (moodle_exception $e) { 1322 $this->assertEquals('invalidsection', $e->errorcode); 1323 } 1324 1325 } 1326 1327 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |