Permalink
Browse files

MDL-37852 phpunit: Tests for repository::can_be_edited_by_user()

  • Loading branch information...
FMCorz authored and stronk7 committed Feb 27, 2013
1 parent 89a9d21 commit da9d3a1205377c027d7b476903bdbd520dd240d0
Showing with 83 additions and 0 deletions.
  1. +83 −0 repository/tests/repository_test.php
@@ -154,4 +154,87 @@ public function test_draftfile_exists() {
}
}
+ public function test_can_be_edited_by_user() {
+ $this->resetAfterTest(true);
+
+ $syscontext = context_system::instance();
+ $course = $this->getDataGenerator()->create_course();
+ $coursecontext = context_course::instance($course->id);
+ $roleid = create_role('A role', 'arole', 'A role', '');
+ set_role_contextlevels($roleid, array($syscontext->contextlevel, $coursecontext->contextlevel));
+ $user = $this->getDataGenerator()->create_user();
+ $this->setUser($user);
+
+ $plugintype = new repository_type('flickr_public');
+ $plugintype->create(true);
+ $params = array(
+ 'name' => 'Flickr Public'
+ );
+
+ // Instance on a site level.
+ $repoid = repository::static_function('flickr_public', 'create', 'flickr_public', 0, $syscontext, $params);
+ $systemrepo = repository::get_instance($repoid);
+
+ role_assign($roleid, $user->id, $syscontext->id);
+ assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext, true);
+ assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertTrue($systemrepo->can_be_edited_by_user());
+
+ assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
+ assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertFalse($systemrepo->can_be_edited_by_user());
+
+ assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
+ assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertFalse($systemrepo->can_be_edited_by_user());
+
+ role_unassign($roleid, $user->id, $syscontext->id);
+ accesslib_clear_all_caches_for_unit_testing();
+
+ // Instance on a course level.
+ $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
+
+ $repoid = repository::static_function('flickr_public', 'create', 'flickr_public', 0, $coursecontext, $params);
+ $courserepo = repository::get_instance($repoid);
+
+ assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
+ assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $coursecontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertTrue($courserepo->can_be_edited_by_user());
+
+ assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertFalse($courserepo->can_be_edited_by_user());
+
+ assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
+ assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+ $this->assertFalse($courserepo->can_be_edited_by_user());
+
+ role_unassign($roleid, $user->id, $coursecontext->id);
+ accesslib_clear_all_caches_for_unit_testing();
+
+ // Instance on a user level.
+ $otheruser = $this->getDataGenerator()->create_user();
+ $otherusercontext = context_user::instance($otheruser->id);
+ role_assign($roleid, $user->id, $syscontext->id);
+ assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
+ accesslib_clear_all_caches_for_unit_testing();
+
+ // Editing someone else's instance.
+ $repoid = repository::static_function('flickr_public', 'create', 'flickr_public', 0, $otherusercontext, $params);
+ $userrepo = repository::get_instance($repoid);
+ $this->assertFalse($userrepo->can_be_edited_by_user());
+
+ // Editing my own instance.
+ $usercontext = context_user::instance($user->id);
+ $repoid = repository::static_function('flickr_public', 'create', 'flickr_public', 0, $usercontext, $params);
+ $userrepo = repository::get_instance($repoid);
+ $this->assertTrue($userrepo->can_be_edited_by_user());
+
+ }
+
}

0 comments on commit da9d3a1

Please sign in to comment.