Skip to content

Commit

Permalink
version update
Browse files Browse the repository at this point in the history
  • Loading branch information
ewallah committed Nov 3, 2020
1 parent 6216228 commit 22b3cf0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 96 deletions.
17 changes: 9 additions & 8 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class format_masonry extends format_topics {
*/
public function get_view_url($section, $options = []) {
$course = $this->get_course();
return new moodle_url('/course/view.php', ['id' => $course->id]);
return new \moodle_url('/course/view.php', ['id' => $course->id]);
}

/**
Expand Down Expand Up @@ -102,17 +102,17 @@ public function course_format_options($foreditform = false) {
}
$courseoptionsedit = [
'numsections' => [
'label' => new lang_string('numberweeks'),
'label' => new \lang_string('numberweeks'),
'element_type' => 'select',
'element_attributes' => [$sectionmenu]],
'hiddensections' => [
'label' => 'hidden1',
'element_type' => 'hidden',
'element_attributes' => [[1 => new lang_string('hiddensectionsinvisible')]]],
'element_attributes' => [[1 => new \lang_string('hiddensectionsinvisible')]]],
'coursedisplay' => [
'label' => 'hidden2',
'element_type' => 'hidden',
'element_attributes' => [[COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single')]]],
'element_attributes' => [[COURSE_DISPLAY_SINGLEPAGE => new \lang_string('coursedisplay_single')]]],
'borderwidth' => [
'label' => get_string('borderwidth', 'format_masonry'),
'element_type' => 'select',
Expand Down Expand Up @@ -158,14 +158,15 @@ public function update_course_format_options($data, $oldcourse = null) {
* @param null|lang_string|string $editlabel
* @return \core\output\inplace_editable
*/
public function inplace_editable_render_section_name($section, $linkifneeded = true,
$editable = null, $edithint = null, $editlabel = null) {
public function inplace_editable_render_section_name(
$section, $linkifneeded = true, $editable = null, $edithint = null, $editlabel = null) {

if (empty($edithint)) {
$edithint = new lang_string('editsectionname', 'format_topics');
$edithint = new \lang_string('editsectionname', 'format_topics');
}
if (empty($editlabel)) {
$title = get_section_name($section->course, $section);
$editlabel = new lang_string('newsectionname', 'format_topics', $title);
$editlabel = new \lang_string('newsectionname', 'format_topics', $title);
}
return parent::inplace_editable_render_section_name($section, $linkifneeded, $editable, $edithint, $editlabel);
}
Expand Down
137 changes: 50 additions & 87 deletions tests/format_masonry_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
namespace format_masonry;

global $CFG;
require_once($CFG->dirroot . '/course/format/masonry/renderer.php');
defined('MOODLE_INTERNAL') || die();

/**
* format_masonry related unit tests
Expand All @@ -35,32 +34,44 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers format_masonry
*/
class format_masonry_testcase extends advanced_testcase {
class masonry_testcase extends \advanced_testcase {

/**
* Load required classes.
*/
public function setUp():void {
// Load the mock info class so that it can be used.
global $CFG;
require_once($CFG->dirroot . '/course/format/masonry/renderer.php');
$this->resetAfterTest(true);
}

/**
* Tests for format_masonry::get_section_name method with default section names.
* @covers format_masonry
*/
public function test_get_section_name() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
$sections = get_fast_modinfo($course)->get_section_info_all();
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
foreach ($sections as $section) {
// Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name.
$this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section));
if ($section->section == 0) {
$sectionname = get_string('section0name', 'format_masonry');
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
} else {
$sectionname = get_string('sectionname', 'format_masonry') . ' ' . $section->section;
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
}
}
}

/**
* Tests for format_masonry::get_section_name method with modified section names.
* @covers format_masonry
*/
public function test_get_section_name_customised() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
Expand All @@ -72,45 +83,21 @@ public function test_get_section_name_customised() {
}

// Requery updated section names then test get_section_name.
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
$sections = get_fast_modinfo($course)->get_section_info_all();
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
foreach ($sections as $section) {
// Assert that with modified section names, get_section_name returns the modified section name.
$this->assertEquals($section->name, $courseformat->get_section_name($section));
}
}

/**
* Tests for format_masonry::get_default_section_name.
* @covers format_masonry
*/
public function test_get_default_section_name() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
if ($section->section == 0) {
$sectionname = get_string('section0name', 'format_masonry');
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
} else {
$sectionname = get_string('sectionname', 'format_masonry') . ' ' . $section->section;
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
}
}
}

/**
* Test web service updating section name
* @covers format_masonry
*/
public function test_update_inplace_editable() {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . '/lib/external/externallib.php');

$this->resetAfterTest();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
Expand All @@ -119,30 +106,26 @@ public function test_update_inplace_editable() {
$section = $modinfo->get_section_info(2);
$USER->editing = true;

try {
core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertEquals('Course or activity not accessible. (Not enrolled)', $e->getMessage());
}
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage('Course or activity not accessible. (Not enrolled)');
\core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');

$teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
$generator->enrol_user($user->id, $course->id, $teacherrole->id);

$res = core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');
$res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
$res = \core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');
$res = \external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $res);
$this->assertEquals('New section name', $res['value']);
$this->assertEquals('New section name', $DB->get_field('course_sections', 'name', ['id' => $section->id]));

$section = $modinfo->get_section_info(1);
core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');
\core_external::update_inplace_editable('format_masonry', 'sectionname', $section->id, 'New section name');
format_masonry_inplace_editable('sectionname', $section->id, 'New section name twice');
$this->assertEquals('New section name twice', $DB->get_field('course_sections', 'name', ['id' => $section->id]));
}

/**
* Test callback updating section name
* @covers format_masonry
*/
public function test_inplace_editable() {
global $DB, $PAGE, $USER;
Expand All @@ -166,17 +149,13 @@ public function test_inplace_editable() {
$this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', ['id' => $section->id]));

// Try updating using callback from mismatching course format.
try {
$tmpl = component_callback('format_weeks', 'inplace_editable', ['sectionname', $section->id, 'New name']);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage()));
}
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage('Can\'t find data record in database');
component_callback('format_weeks', 'inplace_editable', ['sectionname', $section->id, 'New name']);
}

/**
* Test get_default_course_enddate.
* @covers format_masonry
*/
public function test_default_course_enddate() {
global $CFG, $DB;
Expand All @@ -193,14 +172,14 @@ public function test_default_course_enddate() {
'course' => $course,
'category' => $category,
'editoroptions' => [
'context' => context_course::instance($course->id),
'context' => \context_course::instance($course->id),
'subdirs' => 0
],
'returnto' => new moodle_url('/'),
'returnurl' => new moodle_url('/'),
'returnto' => new \moodle_url('/'),
'returnurl' => new \moodle_url('/'),
];

$courseform = new testable_course_edit_form(null, $args);
$courseform = new \testable_course_edit_form(null, $args);
$courseform->definition_after_data();

$enddate = $params['startdate'] + get_config('moodlecourse', 'courseduration');
Expand All @@ -216,7 +195,7 @@ public function test_default_course_enddate() {

/**
* Test renderer.
* @covers format_masonry_renderer
* @covers \format_masonry_renderer
*/
public function test_renderer() {
global $PAGE, $USER;
Expand All @@ -227,8 +206,8 @@ public function test_renderer() {
$generator->get_plugin_generator('mod_forum')->create_instance(['course' => $course->id, 'section' => 1]);
$generator->get_plugin_generator('mod_wiki')->create_instance(['course' => $course->id, 'section' => 1]);
set_section_visible($course->id, 2, 0);
$page = new moodle_page();
$page->set_context(context_course::instance($course->id));
$page = new \moodle_page();
$page->set_context(\context_course::instance($course->id));
$page->set_course($course);
$page->set_pagelayout('standard');
$page->set_pagetype('course-view');
Expand All @@ -254,7 +233,7 @@ public function test_renderer() {
$this->assertStringContainsString('Topic 2', $renderer->section_title_without_link($section, $course));
set_section_visible($course->id, 2, 0);
$USER->editing = true;
$PAGE->set_context(context_course::instance($course->id));
$PAGE->set_context(\context_course::instance($course->id));
$PAGE->set_pagelayout('standard');
$PAGE->set_pagetype('course-view');
$PAGE->set_url('/course/view.php?id=' . $course->id);
Expand Down Expand Up @@ -285,63 +264,46 @@ public function test_renderer() {

/**
* Test upgrade.
* @covers format_masonry
*/
public function test_upgrade() {
global $CFG;
$this->resetAfterTest(true);
require_once($CFG->dirroot . '/course/format/masonry/db/upgrade.php');
require_once($CFG->libdir . '/upgradelib.php');
try {
$this->assertTrue(xmldb_format_masonry_upgrade(time()));
} catch (moodle_exception $e) {
$this->assertEquals(1, preg_match('/^Cannot downgrade/', $e->getMessage()));
}
}

/**
* Test privacy.
* @covers format_masonry\privacy\provider
*/
public function test_privacy() {
$privacy = new \format_masonry\privacy\provider();
$this->assertEquals($privacy->get_reason(), 'privacy:metadata');
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage('Cannot downgrade');
xmldb_format_masonry_upgrade(time());
}

/**
* Test format.
* @covers format_masonry
*/
public function test_format() {
global $CFG, $PAGE;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$format = course_get_format($course);
$this->assertEquals('masonry', $format->get_format());
$this->setAdminUser();
$PAGE->get_renderer('core', 'course');
$PAGE->set_context(context_course::instance($course->id));
$PAGE->set_context(\context_course::instance($course->id));
ob_start();
include_once($CFG->dirroot . '/course/format/masonry/format.php');
ob_end_clean();
}

/**
* Test format editing.
* @covers format_masonry
*/
public function test_format_editing() {
global $CFG, $PAGE, $USER;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$format = course_get_format($course);
$this->assertEquals('masonry', $format->get_format());
$this->setAdminUser();
$USER->editing = true;
$PAGE->get_renderer('core', 'course');
$PAGE->set_context(context_course::instance($course->id));
$PAGE->set_context(\context_course::instance($course->id));
sesskey();
$_POST['marker'] = 2;
ob_start();
Expand All @@ -351,19 +313,20 @@ public function test_format_editing() {

/**
* Test other.
* @covers format_masonry
*/
public function test_other() {
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course(['numsections' => 5, 'format' => 'masonry'], ['createsections' => true]);
$sections = get_fast_modinfo($course)->get_section_info_all();
$format = course_get_format($course);
$data = new stdClass();
$data = new \stdClass();
$data->bordercolor = '#FFF';
$data->backcolor = '#000';
$format->update_course_format_options($data, $course);
$this->assertCount(6, $format->course_format_options());
$this->assertTrue($format->allow_stealth_module_visibility(null, null));
$this->assertCount(6, $format->get_config_for_external());
$this->assertNotEmpty(format_masonry_inplace_editable('sectionname', $sections[1]->id, 'newname'));
}
}

0 comments on commit 22b3cf0

Please sign in to comment.