Skip to content

Commit

Permalink
Add PHPUnit test (#571)
Browse files Browse the repository at this point in the history
Add PHPUnit test for deleting a page containing elements.  Previously
this action resulted in an error.
  • Loading branch information
leonstr authored and mdjnelson committed Dec 29, 2023
1 parent 501eedc commit 46fd8c4
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/page_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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/>.

/**
* Contains tests for template's page operations.
*
* @package mod_customcert
* @copyright 2023 Leon Stringer <[email protected]>
* @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 <[email protected]>
* @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);
}
}

0 comments on commit 46fd8c4

Please sign in to comment.