MDL-40871 - mod_data: Unit tests for data_delete_record()
[moodle.git] / mod / data / tests / lib_test.php
CommitLineData
e727e8ff
AG
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for lib.php
19 *
20 * @package mod_data
21 * @category phpunit
22 * @copyright 2013 Adrian Greeve
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/mod/data/lib.php');
30
31/**
32 * Unit tests for lib.php
33 *
34 * @package mod_data
35 * @copyright 2013 Adrian Greeve
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class data_lib_testcase extends advanced_testcase {
39
40 function test_data_delete_record() {
41 global $DB;
42
43 $this->resetAfterTest();
44
45 // Create a record for deleting.
46 $this->setAdminUser();
47 $course = $this->getDataGenerator()->create_course();
48 $record = new stdClass();
49 $record->course = $course->id;
50 $record->name = "Mod data delete test";
51 $record->intro = "Some intro of some sort";
52
53 $module = $this->getDataGenerator()->create_module('data', $record);
54
55 $field = data_get_field_new('text', $module);
56
57 $fielddetail = new stdClass();
58 $fielddetail->d = $module->id;
59 $fielddetail->mode = 'add';
60 $fielddetail->type = 'text';
61 $fielddetail->sesskey = sesskey();
62 $fielddetail->name = 'Name';
63 $fielddetail->description = 'Some name';
64
65 $field->define_field($fielddetail);
66 $field->insert_field();
67 $recordid = data_add_record($module);
68
69 $datacontent = array();
70 $datacontent['fieldid'] = $field->field->id;
71 $datacontent['recordid'] = $recordid;
72 $datacontent['content'] = 'Asterix';
73
74 $contentid = $DB->insert_record('data_content', $datacontent);
75 $cm = get_coursemodule_from_instance('data', $module->id, $course->id);
76
77 // Check to make sure that we have a database record.
78 $data = $DB->get_records('data', array('id' => $module->id));
79 $this->assertEquals(1, count($data));
80
81 $datacontent = $DB->get_records('data_content', array('id' => $contentid));
82 $this->assertEquals(1, count($datacontent));
83
84 $datafields = $DB->get_records('data_fields', array('id' => $field->field->id));
85 $this->assertEquals(1, count($datafields));
86
87 $datarecords = $DB->get_records('data_records', array('id' => $recordid));
88 $this->assertEquals(1, count($datarecords));
89
90 // Test to see if a failed delete returns false.
91 $result = data_delete_record(8798, $module, $course->id, $cm->id);
92 $this->assertFalse($result);
93
94 // Delete the record.
95 $result = data_delete_record($recordid, $module, $course->id, $cm->id);
96
97 // Check that all of the record is gone.
98 $datacontent = $DB->get_records('data_content', array('id' => $contentid));
99 $this->assertEquals(0, count($datacontent));
100
101 $datarecords = $DB->get_records('data_records', array('id' => $recordid));
102 $this->assertEquals(0, count($datarecords));
103
104 // Make sure the function returns true on a successful deletion.
105 $this->assertTrue($result);
106 }
107}