/** * Test the filter_matches function. * * @dataProvider filter_matches_provider * @param array $filtervalues The filter values * @param string $currenttheme The name of the current theme * @param boolean $expected Whether the tour is expected to match */ public function test_filter_matches($filtervalues, $currenttheme, $expected) { global $PAGE; $filtername = \tool_usertours\local\filter\theme::class; // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); if ($filtervalues !== null) { $tour->set_filter_values('theme', $filtervalues); } $PAGE->theme->name = $currenttheme; // Note: The theme filter does not use the context. $this->assertEquals($expected, $filtername::filter_matches($tour, \context_system::instance())); }
/** * Test the filter_matches function when one user has multiple roles. */ public function test_filter_matches_multiple_role_one_user() { $context = \context_course::instance($this->course->id); $roles = [$this->roles['student']]; $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->roles['teacher']); // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); $tour->set_filter_values('role', $roles); // Note: The role filter does not use the context. foreach ($this->testroles as $role) { $this->setUser($this->{$role}); if ($role === 'student') { $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); } else { $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); } } // The admin should always be able to view too. $this->setAdminUser(); $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); }
/** * Test the filter_matches function when an admin user has multiple roles. */ public function test_filter_matches_multiple_role_admin_user() { global $USER; $context = \context_course::instance($this->course->id); $roles = [\tool_usertours\local\filter\role::ROLE_SITEADMIN]; $this->setAdminUser(); $this->getDataGenerator()->enrol_user($USER->id, $this->course->id, $this->roles['student']); // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); $tour->set_filter_values('role', $roles); // The admin can view this one because it's only aimed at them. $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); }