diff --git a/lib.php b/lib.php index 982af15..795999d 100644 --- a/lib.php +++ b/lib.php @@ -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]); } /** @@ -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', @@ -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); } diff --git a/tests/format_masonry_test.php b/tests/format_masonry_test.php index 0373978..0a84e7d 100644 --- a/tests/format_masonry_test.php +++ b/tests/format_masonry_test.php @@ -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 @@ -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]); @@ -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); @@ -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; @@ -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; @@ -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'); @@ -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; @@ -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'); @@ -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); @@ -285,43 +264,28 @@ 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(); @@ -329,11 +293,9 @@ public function test_format() { /** * 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); @@ -341,7 +303,7 @@ public function test_format_editing() { $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(); @@ -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')); } } \ No newline at end of file diff --git a/tests/privacy_test.php b/tests/privacy_test.php new file mode 100644 index 0000000..b28dcbe --- /dev/null +++ b/tests/privacy_test.php @@ -0,0 +1,51 @@ +. + +/** + * Course format masonry privacy tests. + * + * @package format_masonry + * @copyright 2017 eWallah.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace format_masonry\privacy; + +defined('MOODLE_INTERNAL') || die(); + +use \core_privacy\tests\provider_testcase; + +/** + * Course format masonry privacy tests. + * + * @package format_masonry + * @copyright 2017 eWallah.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass format_masonry + */ +class priv_testcase extends provider_testcase { + + /** + * Test returning metadata. + * @covers format_masonry\privacy\provider + */ + public function test_get_metadata() { + $collection = new \core_privacy\local\metadata\collection('format_masonry'); + $reason = \format_masonry\privacy\provider::get_reason($collection); + $this->assertEquals($reason, 'privacy:metadata'); + $this->assertStringContainsString('does not store', get_string($reason, 'format_masonry')); + } +} \ No newline at end of file diff --git a/version.php b/version.php index 10be01e..57a4a76 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020102300; +$plugin->version = 2020110300; $plugin->requires = 2019111800; $plugin->component = 'format_masonry'; $plugin->release = '3.8+';