Merge branch 'MDL-63136-master' of git://github.com/rezaies/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 11 Sep 2018 21:52:02 +0000 (23:52 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 11 Sep 2018 21:52:02 +0000 (23:52 +0200)
1  2 
mod/data/tests/lib_test.php

@@@ -1314,6 -1314,74 +1314,74 @@@ class mod_data_lib_testcase extends adv
          $this->assertEquals([$datarecor1did, $datarecor2did], $updates->entries->itemids, '', 0, 10, true);
      }
  
+     public function test_data_core_calendar_provide_event_action_in_hidden_section() {
+         global $CFG;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a student.
+         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
+                 'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Set sections 0 as hidden.
+         set_section_visible($course->id, 0, 0);
+         // Now, log out.
+         $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event for the student.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
+         // Confirm the event is not shown at all.
+         $this->assertNull($actionevent);
+     }
+     public function test_data_core_calendar_provide_event_action_for_non_user() {
+         global $CFG;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
+             'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Now, log out.
+         $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory);
+         // Confirm the event is not shown at all.
+         $this->assertNull($actionevent);
+     }
      public function test_data_core_calendar_provide_event_action_open() {
          $this->resetAfterTest();
  
          $this->assertTrue($actionevent->is_actionable());
      }
  
+     public function test_data_core_calendar_provide_event_action_open_for_user() {
+         global $CFG;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a student.
+         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
+             'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Now log out.
+         $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event for the student.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
+         // Confirm the event was decorated.
+         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
+         $this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
+         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
+         $this->assertEquals(1, $actionevent->get_item_count());
+         $this->assertTrue($actionevent->is_actionable());
+     }
      public function test_data_core_calendar_provide_event_action_closed() {
          $this->resetAfterTest();
  
          $this->assertNull($actionevent);
      }
  
+     public function test_data_core_calendar_provide_event_action_closed_for_user() {
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a student.
+         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
+             'timeavailableto' => time() - DAYSECS));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Now log out.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event for the student.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
+         // No event on the dashboard if module is closed.
+         $this->assertNull($actionevent);
+     }
      public function test_data_core_calendar_provide_event_action_open_in_future() {
          $this->resetAfterTest();
  
          $this->assertFalse($actionevent->is_actionable());
      }
  
+     public function test_data_core_calendar_provide_event_action_open_in_future_for_user() {
+         global $CFG;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a student.
+         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
+             'timeavailablefrom' => time() + DAYSECS));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Now log out.
+         $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event for the student.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
+         // Confirm the event was decorated.
+         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
+         $this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
+         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
+         $this->assertEquals(1, $actionevent->get_item_count());
+         $this->assertFalse($actionevent->is_actionable());
+     }
      public function test_data_core_calendar_provide_event_action_no_time_specified() {
          $this->resetAfterTest();
  
          $this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
          $this->assertEquals(1, $actionevent->get_item_count());
+         $this->assertTrue($actionevent->is_actionable());
+     }
+     public function test_data_core_calendar_provide_event_action_no_time_specified_for_user() {
+         global $CFG;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Create a course.
+         $course = $this->getDataGenerator()->create_course();
+         // Create a student.
+         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+         // Create a database activity.
+         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id));
+         // Create a calendar event.
+         $event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
+         // Now log out.
+         $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+         $this->setUser();
+         // Create an action factory.
+         $factory = new \core_calendar\action_factory();
+         // Decorate action event for the student.
+         $actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
+         // Confirm the event was decorated.
+         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
+         $this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
+         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
+         $this->assertEquals(1, $actionevent->get_item_count());
          $this->assertTrue($actionevent->is_actionable());
      }
  
          $this->assertNull($min);
          $this->assertNull($max);
      }
 +
 +    /**
 +     * A user who does not have capabilities to add events to the calendar should be able to create an database.
 +     */
 +    public function test_creation_with_no_calendar_capabilities() {
 +        $this->resetAfterTest();
 +        $course = self::getDataGenerator()->create_course();
 +        $context = context_course::instance($course->id);
 +        $user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
 +        $roleid = self::getDataGenerator()->create_role();
 +        self::getDataGenerator()->role_assign($roleid, $user->id, $context->id);
 +        assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
 +        $generator = self::getDataGenerator()->get_plugin_generator('mod_data');
 +        // Create an instance as a user without the calendar capabilities.
 +        $this->setUser($user);
 +        $time = time();
 +        $params = array(
 +            'course' => $course->id,
 +            'timeavailablefrom' => $time + 200,
 +            'timeavailableto' => $time + 2000,
 +            'timeviewfrom' => $time + 400,
 +            'timeviewto' => $time + 2000,
 +        );
 +        $generator->create_instance($params);
 +    }
  }