diff --git a/tests/page_test.php b/tests/page_test.php new file mode 100644 index 00000000..b864f621 --- /dev/null +++ b/tests/page_test.php @@ -0,0 +1,68 @@ +. + +/** + * Contains tests for template's page operations. + * + * @package mod_customcert + * @copyright 2023 Leon Stringer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_customcert\event; + +/** + * Contains tests for template's page operations. + * + * @package mod_customcert + * @copyright 2023 Leon Stringer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class page_test extends \advanced_testcase { + public function setUp(): void { + $this->resetAfterTest(); + } + + /** + * Check deleting a non-empty page has no errors. + * 1. Create a template. + * 2. Add a second page. + * 3. Add an element to the second page. + * 4. Delete the second page. + * Previously the above scenario resulted in an error (#571). + */ + public function test_delete_non_empty_page(): void { + global $DB; + + $template = \mod_customcert\template::create('Test name', \context_system::instance()->id); + + // Add a second page and add an element to it. + $page2id = $template->add_page(); + $element = new \stdClass(); + $element->pageid = $page2id; + $element->name = 'Image'; + $element->element = 'image'; + $elementid = $DB->insert_record('customcert_elements', $element); + + $template->delete_page($page2id); + + $records = $DB->get_records('customcert_elements', array('pageid' => $page2id)); + $this->assertCount(0, $records); + + $records = $DB->get_records('customcert_pages', array('id' => $page2id)); + $this->assertCount(0, $records); + } +}