Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed May 11, 2024
1 parent b372661 commit 54ef402
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function adleradaptivity_delete_instance(int $instance_id): bool {
$transaction->allow_commit();
} catch (Exception $e) {
$logger->error('Could not delete adleradaptivity instance with id ' . $instance_id);
$logger->error($e->getMessage());
$transaction->rollback($e);
try {
$transaction->rollback($e);
Expand Down
16 changes: 8 additions & 8 deletions tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use mod_adleradaptivity\external\answer_questions;
use mod_adleradaptivity\external\external_test_helpers;
use mod_adleradaptivity\lib\adler_testcase;
use mod_adleradaptivity\local\db\adleradaptivity_attempt_repository;
use mod_adleradaptivity\local\db\adleradaptivity_question_repository;

global $CFG;
require_once($CFG->dirroot . '/mod/adleradaptivity/tests/lib/adler_testcase.php');
Expand Down Expand Up @@ -129,10 +131,9 @@ public function test_delete_instance_failure() {
// Try to delete a non-existing instance.
$nonExistingId = 999999; // This ID should be non-existing.

$result = adleradaptivity_delete_instance($nonExistingId);
$this->expectException(moodle_exception::class);

// Check that the method returned false.
$this->assertFalse($result);
$result = adleradaptivity_delete_instance($nonExistingId);

// Ensure that no instances were deleted.
$this->assertCount(0, $DB->get_records('adleradaptivity'));
Expand All @@ -159,22 +160,21 @@ public function test_delete_instance_failure_due_to_question_deletion_failure()
$answer_question_result = answer_questions::execute($answerdata[0], $answerdata[1]);

// Mock the repository object
$mockRepo = Mockery::mock('alias:mod_adleradaptivity\local\db\adleradaptivity_question_repository');
$mockRepo = Mockery::mock('overload:' . adleradaptivity_question_repository::class);
// Make the method throw an exception
$mockRepo->shouldReceive('delete_question_by_id')->andThrow(new Exception('Could not delete'));
$mockRepo->shouldReceive('delete_question_by_id')->andThrow(new moodle_exception('Could not delete'));

// verify created elements before deletion
$this->assertCount(1, $DB->get_records('adleradaptivity'));
$this->assertCount(2, $DB->get_records('adleradaptivity_tasks'));
$this->assertCount(1, $DB->get_records('adleradaptivity_questions'));
$this->assertCount(1, $DB->get_records('adleradaptivity_attempts'));

$this->expectException(moodle_exception::class);

// Try to delete the complex instance.
$result = adleradaptivity_delete_instance($complex_adleradaptivity_module['module']->id);

// Check that the method returned false.
$this->assertFalse($result);

// Ensure that no instances were deleted.
$this->assertCount(1, $DB->get_records('adleradaptivity'));
$this->assertCount(2, $DB->get_records('adleradaptivity_tasks'));
Expand Down

0 comments on commit 54ef402

Please sign in to comment.