first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,223 @@
@mod @mod_url @core_completion
Feature: View activity completion information in the URL resource
In order to have visibility of URL completion requirements
As a student
I need to be able to view my URL completion progress
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Vinnie | Student1 | student1@example.com |
| teacher1 | Darrell | Teacher1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category | enablecompletion | showcompletionconditions |
| Course 1 | C1 | 0 | 1 | 1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| teacher1 | C1 | editingteacher |
And the following config values are set as admin:
| displayoptions | 0,1,2,3,4,5,6 | url |
Scenario: View automatic completion items in automatic display mode as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 0 |
When I am on the "Music history" "url activity" page logged in as teacher1
Then "Music history" "link" should exist
And I should see "Click on Music history to open the resource."
And "Music history" should have the "View" completion condition
Scenario: View automatic completion items in automatic display mode as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 0 |
When I am on the "Music history" "url activity" page logged in as student1
Then the "View" completion condition of "Music history" is displayed as "done"
Scenario: View automatic completion items in embed display mode as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 1 |
When I am on the "Music history" "url activity" page logged in as teacher1
Then "Music history" should have the "View" completion condition
Scenario: View automatic completion items in embed display mode as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 1 |
When I am on the "Music history" "url activity" page logged in as student1
Then the "View" completion condition of "Music history" is displayed as "done"
@javascript
Scenario: View automatic completion items in open display mode as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 5 |
When I am on the "Music history" "url activity" page logged in as teacher1
And I am on the "Course 1" course page
Then "Music history" should have the "View" completion condition
@javascript
Scenario: View automatic completion items in open display mode as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 5 |
When I am on the "Music history" "url activity" page logged in as student1
And I am on the "Course 1" course page
Then the "View" completion condition of "Music history" is displayed as "done"
Scenario: View automatic completion items in pop-up display mode as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 6 |
| popupwidth | 620 |
| popupheight | 450 |
When I am on the "Music history" "url activity" page logged in as student1
Then "Music history" should have the "View" completion condition
Scenario: View automatic completion items in pop-up display mode as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 2 |
| completionview | 1 |
| display | 6 |
| popupwidth | 620 |
| popupheight | 450 |
When I am on the "Music history" "url activity" page logged in as student1
Then the "View" completion condition of "Music history" is displayed as "done"
@javascript
Scenario: Use manual completion with automatic URL as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 1 |
| completionview | 1 |
| display | 0 |
When I am on the "Music history" "url activity" page logged in as teacher1
Then the manual completion button for "Music history" should be disabled
@javascript
Scenario: Use manual completion with automatic URL as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 1 |
| completionview | 1 |
| display | 0 |
When I am on the "Course 1" course page logged in as student1
Then the manual completion button of "Music history" is displayed as "Mark as done"
And I toggle the manual completion state of "Music history"
And the manual completion button of "Music history" is displayed as "Done"
@javascript
Scenario Outline: The Mark as done completion condition will be shown on the course page for Open, In pop-up and New window display mode if the Show activity completion conditions is set to No as teacher
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 1 |
| completionview | 1 |
| display | <display> |
| popupwidth | 620 |
| popupheight | 450 |
When I am on the "Course 1" course page logged in as teacher1
Then "Music history" should have the "Mark as done" completion condition
Examples:
| display | description |
| 0 | Auto |
| 6 | Popup |
| 3 | New |
@javascript
Scenario Outline: The manual completion button will be shown on the course page for Open, In pop-up and New window display mode if the Show activity completion conditions is set to No as student
Given the following "activity" exists:
| activity | url |
| course | C1 |
| idnumber | Music history |
| name | Music history |
| intro | URL description |
| externalurl | https://moodle.org/ |
| completion | 1 |
| completionview | 1 |
| display | <display> |
| popupwidth | 620 |
| popupheight | 450 |
When I am on the "Course 1" course page logged in as student1
Then the manual completion button for "Music history" should exist
And the manual completion button of "Music history" is displayed as "Mark as done"
And I toggle the manual completion state of "Music history"
And the manual completion button of "Music history" is displayed as "Done"
Examples:
| display | description |
| 0 | Auto |
| 6 | Popup |
| 3 | New |
+52
View File
@@ -0,0 +1,52 @@
@mod @mod_url @javascript
Feature: Manage URL variables
In order to maintain privacy for URLs
As a teacher
I need to be able to manage URL variables safely
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
Scenario: Disabling URL variables hides Role names as URL variables check box
Given the following config values are set as admin:
| allowvariables | 1 | url |
And I log in as "admin"
And I navigate to "Plugins > Activity modules > URL" in site administration
When I click on "Allow URL variables" "checkbox"
Then I should not see "Role names as URL variables"
And I click on "Allow URL variables" "checkbox"
And I should see "Role names as URL variables"
Scenario: Disable the use of URL variables
Given the following config values are set as admin:
| allowvariables | 0 | url |
When I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
And I add a "URL" to section "1" using the activity chooser
Then I should not see "URL variables"
Scenario: Enable the use of URL variables without role names
Given the following config values are set as admin:
| allowvariables | 1 | url |
| rolesinparams | 0 | url |
When I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
And I add a "URL" to section "1" using the activity chooser
Then I should see "URL variables"
And I expand all fieldsets
And I should see "Full site name" in the "id_variable_0" "select"
But I should not see "Roles" in the "id_variable_0" "select"
Scenario: Enable the use of URL variables with role names
Given the following config values are set as admin:
| allowvariables | 1 | url |
| rolesinparams | 1 | url |
When I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
And I add a "URL" to section "1" using the activity chooser
Then I should see "URL variables"
And I expand all fieldsets
And I should see "Full site name" in the "id_variable_0" "select"
And I should see "Your word for 'Student'" in the "id_variable_0" "select"
+229
View File
@@ -0,0 +1,229 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_url;
use core_external\external_api;
use externallib_advanced_testcase;
use mod_url_external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External mod_url functions unit tests
*
* @package mod_url
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class externallib_test extends externallib_advanced_testcase {
/**
* Test view_url
*/
public function test_view_url(): void {
global $DB;
$this->resetAfterTest(true);
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
$context = \context_module::instance($url->cmid);
$cm = get_coursemodule_from_instance('url', $url->id);
// Test invalid instance id.
try {
mod_url_external::view_url(0);
$this->fail('Exception expected due to invalid mod_url instance id.');
} catch (\moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_url_external::view_url($url->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (\moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_url_external::view_url($url->id);
$result = external_api::clean_returnvalue(mod_url_external::view_url_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/url:view', CAP_PROHIBIT, $studentrole->id, $context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
\course_modinfo::clear_instance_cache();
try {
mod_url_external::view_url($url->id);
$this->fail('Exception expected due to missing capability.');
} catch (\moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
/**
* Test test_mod_url_get_urls_by_courses
*/
public function test_mod_url_get_urls_by_courses(): void {
global $DB;
$this->resetAfterTest(true);
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
// First url.
$record = new \stdClass();
$record->course = $course1->id;
$url1 = self::getDataGenerator()->create_module('url', $record);
// Second url.
$record = new \stdClass();
$record->course = $course2->id;
$url2 = self::getDataGenerator()->create_module('url', $record);
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
self::setUser($student);
$returndescription = mod_url_external::get_urls_by_courses_returns();
// Create what we expect to be returned when querying the two courses.
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'lang',
'externalurl', 'display', 'displayoptions', 'parameters', 'timemodified', 'section', 'visible', 'groupmode',
'groupingid');
// Add expected coursemodule and data.
$url1->coursemodule = $url1->cmid;
$url1->introformat = 1;
$url1->section = 0;
$url1->visible = true;
$url1->groupmode = 0;
$url1->groupingid = 0;
$url1->introfiles = [];
$url1->lang = '';
$url2->coursemodule = $url2->cmid;
$url2->introformat = 1;
$url2->section = 0;
$url2->visible = true;
$url2->groupmode = 0;
$url2->groupingid = 0;
$url2->introfiles = [];
$url2->lang = '';
foreach ($expectedfields as $field) {
$expected1[$field] = $url1->{$field};
$expected2[$field] = $url2->{$field};
}
$expectedurls = array($expected2, $expected1);
// Call the external function passing course ids.
$result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedurls, $result['urls']);
$this->assertCount(0, $result['warnings']);
// Call the external function without passing course id.
$result = mod_url_external::get_urls_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedurls, $result['urls']);
$this->assertCount(0, $result['warnings']);
// Add a file to the intro.
$filename = "file.txt";
$filerecordinline = array(
'contextid' => \context_module::instance($url2->cmid)->id,
'component' => 'mod_url',
'filearea' => 'intro',
'itemid' => 0,
'filepath' => '/',
'filename' => $filename,
);
$fs = get_file_storage();
$timepost = time();
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
$result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertCount(1, $result['urls'][0]['introfiles']);
$this->assertEquals($filename, $result['urls'][0]['introfiles'][0]['filename']);
// Unenrol user from second course and alter expected urls.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expectedurls);
// Call the external function without passing course id.
$result = mod_url_external::get_urls_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedurls, $result['urls']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_url_external::get_urls_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_url data generator.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_url data generator class.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_url_generator extends testing_module_generator {
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once($CFG->libdir.'/resourcelib.php');
// Add default values for url.
$record = (array)$record + array(
'display' => RESOURCELIB_DISPLAY_AUTO,
'externalurl' => 'http://moodle.org/',
);
return parent::create_instance($record, (array)$options);
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_url;
/**
* Genarator tests class for mod_url.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_test extends \advanced_testcase {
public function test_create_instance(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('url', array('course' => $course->id)));
$url = $this->getDataGenerator()->create_module('url', array('course' => $course));
$records = $DB->get_records('url', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($url->id, $records));
$params = array('course' => $course->id, 'name' => 'Another url');
$url = $this->getDataGenerator()->create_module('url', $params);
$records = $DB->get_records('url', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another url', $records[$url->id]->name);
}
}
+268
View File
@@ -0,0 +1,268 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for some mod URL lib stuff.
*
* @package mod_url
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_url;
defined('MOODLE_INTERNAL') || die();
/**
* mod_url tests
*
* @package mod_url
* @category phpunit
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lib_test extends \advanced_testcase {
/**
* Prepares things before this test case is initialised
* @return void
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/mod/url/lib.php');
require_once($CFG->dirroot . '/mod/url/locallib.php');
}
/**
* Tests the url_appears_valid_url function
* @return void
*/
public function test_url_appears_valid_url(): void {
$this->assertTrue(url_appears_valid_url('http://example'));
$this->assertTrue(url_appears_valid_url('http://www.example.com'));
$this->assertTrue(url_appears_valid_url('http://www.examplé.com'));
$this->assertTrue(url_appears_valid_url('http://💩.la'));
$this->assertTrue(url_appears_valid_url('http://香港大學.香港'));
$this->assertTrue(url_appears_valid_url('http://وزارة-الأتصالات.مصر'));
$this->assertTrue(url_appears_valid_url('http://www.теннис-алт.рф'));
$this->assertTrue(url_appears_valid_url('http://имена.бг'));
$this->assertTrue(url_appears_valid_url('http://straße.de'));
$this->assertTrue(url_appears_valid_url('http://キース.コム'));
$this->assertTrue(url_appears_valid_url('http://太亞.中国'));
$this->assertTrue(url_appears_valid_url('http://www.რეგისტრაცია.გე'));
$this->assertTrue(url_appears_valid_url('http://уміц.укр'));
$this->assertTrue(url_appears_valid_url('http://현대.한국'));
$this->assertTrue(url_appears_valid_url('http://мон.мон'));
$this->assertTrue(url_appears_valid_url('http://тест.қаз'));
$this->assertTrue(url_appears_valid_url('http://рнидс.срб'));
$this->assertTrue(url_appears_valid_url('http://اسماء.شبكة'));
$this->assertTrue(url_appears_valid_url('http://www.informationssäkerhet.se'));
$this->assertTrue(url_appears_valid_url('http://москва.рф/services'));
$this->assertTrue(url_appears_valid_url('http://detdumærker.dk'));
$this->assertTrue(url_appears_valid_url('http://www.exa-mple2.com'));
$this->assertTrue(url_appears_valid_url('http://www.example.com/~nobody/index.html'));
$this->assertTrue(url_appears_valid_url('http://www.example.com#hmm'));
$this->assertTrue(url_appears_valid_url('http://www.example.com/#hmm'));
$this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt'));
$this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt#hmmmm'));
$this->assertTrue(url_appears_valid_url('http://www.example.com/index.php?xx=yy&zz=aa'));
$this->assertTrue(url_appears_valid_url('http://www.example.com:80/index.php?xx=yy&zz=aa'));
$this->assertTrue(url_appears_valid_url('https://user:password@www.example.com/žlutý koníček/lala.txt'));
$this->assertTrue(url_appears_valid_url('ftp://user:password@www.example.com/žlutý koníček/lala.txt'));
$this->assertFalse(url_appears_valid_url('http:example.com'));
$this->assertFalse(url_appears_valid_url('http:/example.com'));
$this->assertFalse(url_appears_valid_url('http://'));
$this->assertFalse(url_appears_valid_url('http://www.exa mple.com'));
$this->assertFalse(url_appears_valid_url('http://@www.example.com'));
$this->assertFalse(url_appears_valid_url('http://user:@www.example.com'));
$this->assertTrue(url_appears_valid_url('lalala://@:@/'));
}
/**
* Test url_view
* @return void
*/
public function test_url_view(): void {
global $CFG;
$CFG->enablecompletion = 1;
$this->resetAfterTest();
// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1));
$context = \context_module::instance($url->cmid);
$cm = get_coursemodule_from_instance('url', $url->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$this->setAdminUser();
url_view($url, $course, $cm, $context);
$events = $sink->get_events();
// 2 additional events thanks to completion.
$this->assertCount(3, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Check completion status.
$completion = new \completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
}
/**
* Test mod_url_core_calendar_provide_event_action with user override
*/
public function test_url_core_calendar_provide_event_action_user_override(): void {
global $CFG, $USER;
$this->resetAfterTest();
$this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$CFG->enablecompletion = 1;
// Create the activity.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
// Get some additional data.
$cm = get_coursemodule_from_instance('url', $url->id);
// Create a calendar event.
$event = $this->create_action_event($course->id, $url->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
// Mark the activity as completed.
$completion = new \completion_info($course);
$completion->set_module_viewed($cm);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_url_core_calendar_provide_event_action($event, $factory, $USER->id);
// Decorate action with a userid override.
$actionevent2 = mod_url_core_calendar_provide_event_action($event, $factory, $user->id);
// Ensure result was null because it has been marked as completed for the associated user.
// Logic was brought across from the "_already_completed" function.
$this->assertNull($actionevent);
// Confirm the event was decorated.
$this->assertNotNull($actionevent2);
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
$this->assertEquals(get_string('view'), $actionevent2->get_name());
$this->assertInstanceOf('moodle_url', $actionevent2->get_url());
$this->assertEquals(1, $actionevent2->get_item_count());
$this->assertTrue($actionevent2->is_actionable());
}
public function test_url_core_calendar_provide_event_action(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create the activity.
$course = $this->getDataGenerator()->create_course();
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $url->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('view'), $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_url_core_calendar_provide_event_action_already_completed(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$CFG->enablecompletion = 1;
// Create the activity.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
// Get some additional data.
$cm = get_coursemodule_from_instance('url', $url->id);
// Create a calendar event.
$event = $this->create_action_event($course->id, $url->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
// Mark the activity as completed.
$completion = new \completion_info($course);
$completion->set_module_viewed($cm);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
// Ensure result was null.
$this->assertNull($actionevent);
}
/**
* Creates an action event.
*
* @param int $courseid The course id.
* @param int $instanceid The instance id.
* @param string $eventtype The event type.
* @return bool|calendar_event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
$event = new \stdClass();
$event->name = 'Calendar event';
$event->modulename = 'url';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();
return \calendar_event::create($event);
}
}