Skip to content

Commit

Permalink
MDL-57455 mod_data: Added PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhancox authored and mdjnelson committed Oct 12, 2017
1 parent f4c5328 commit ee9a270
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 3 deletions.
17 changes: 16 additions & 1 deletion mod/data/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,24 @@ public function create_field(stdClass $record = null, $data = null) {
*
* @param mod_data $data
* @param array $contents
* @param int $groupid
* @param array $tags
* @param array $options
* @return data_field_{type}
*/
public function create_entry($data, array $contents, $groupid = 0) {
public function create_entry($data, array $contents, $groupid = 0, $tags = [], array $options = null) {
global $DB;

$this->databaserecordcount++;

$recordid = data_add_record($data, $groupid);

if (isset($options['approved'])) {
data_approve_entry($recordid, !empty($options['approved']));
} else {
$approved = null;
}

$fields = $DB->get_records('data_fields', array('dataid' => $data->id));

// Validating whether required field are filled.
Expand Down Expand Up @@ -309,6 +318,12 @@ public function create_entry($data, array $contents, $groupid = 0) {
}
}

if (!empty($tags)) {
$cm = get_coursemodule_from_instance('data', $data->id);
core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid,
context_module::instance($cm->id), $tags);
}

return $recordid;
}
}
8 changes: 6 additions & 2 deletions mod/data/tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ public function test_create_entry() {
$fieldcontents[$fieldrecord->id] = $contents[$count++];
}

$datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data, $fieldcontents,
$groupa->id);
$tags = ['Cats', 'mice'];

$datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data,
$fieldcontents, $groupa->id, $tags);

$this->assertEquals(1, $DB->count_records('data_records', array('dataid' => $data->id)));
$this->assertEquals(count($contents), $DB->count_records('data_content', array('recordid' => $datarecordid)));
Expand Down Expand Up @@ -229,5 +231,7 @@ public function test_create_entry() {
$this->assertEquals($contents[$contentstartid]->content1, '1');
$this->assertEquals($contents[++$contentstartid]->content, 'http://example.url');
$this->assertEquals($contents[$contentstartid]->content1, 'sampleurl');
$this->assertEquals(array('Cats', 'mice'),
array_values(core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid)));
}
}
265 changes: 265 additions & 0 deletions mod/data/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,271 @@ public function test_data_view() {
$this->assertEquals(1, $completiondata->completionstate);
}

public function test_mod_data_get_tagged_records() {
$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course1 = $this->getDataGenerator()->create_course();

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
$field1 = $datagenerator->create_field($fieldrecord, $data1);

$datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
$datagenerator->create_entry($data1, [$field1->field->id => 'value12'], 0, ['Cats', 'mice']);
$datagenerator->create_entry($data1, [$field1->field->id => 'value13'], 0, ['Cats']);
$datagenerator->create_entry($data1, [$field1->field->id => 'value14'], 0);

$tag = core_tag_tag::get_by_name(0, 'Cats');

// Admin can see everything.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);
$this->assertContains('value11', $res->content);
$this->assertContains('value12', $res->content);
$this->assertContains('value13', $res->content);
$this->assertNotContains('value14', $res->content);
}

public function test_mod_data_get_tagged_records_approval() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course2 = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id));
$field1 = $datagenerator->create_field($fieldrecord, $data1);
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course2->id, 'approval' => true));
$field2 = $datagenerator->create_field($fieldrecord, $data2);

$record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
$record21 = $datagenerator->create_entry($data2, [$field2->field->id => 'value21'], 0, ['Cats'], ['approved' => false]);
$tag = core_tag_tag::get_by_name(0, 'Cats');

// Admin can see everything.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);
$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertEmpty($res->prevpageurl);
$this->assertEmpty($res->nextpageurl);

// Create and enrol a user.
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
$this->setUser($student);

// User can search data records inside a course.
core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertNotContains('value21', $res->content);

$recordtoupdate = new stdClass();
$recordtoupdate->id = $record21;
$recordtoupdate->approved = true;
$DB->update_record('data_records', $recordtoupdate);

core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
}

public function test_mod_data_get_tagged_records_time() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course2 = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';

$timefrom = time() - YEARSECS;
$timeto = time() - WEEKSECS;

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
$field1 = $datagenerator->create_field($fieldrecord, $data1);
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course2->id,
'timeviewfrom' => $timefrom,
'timeviewto' => $timeto));
$field2 = $datagenerator->create_field($fieldrecord, $data2);
$record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
$record21 = $datagenerator->create_entry($data2, [$field2->field->id => 'value21'], 0, ['Cats']);
$tag = core_tag_tag::get_by_name(0, 'Cats');

// Admin can see everything.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);
$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertEmpty($res->prevpageurl);
$this->assertEmpty($res->nextpageurl);

// Create and enrol a user.
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
$this->setUser($student);

// User can search data records inside a course.
core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertNotContains('value21', $res->content);

$data2->timeviewto = time() + YEARSECS;
$DB->update_record('data', $data2);

core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
}

public function test_mod_data_get_tagged_records_course_enrolment() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course2 = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
$field1 = $datagenerator->create_field($fieldrecord, $data1);
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course2->id));
$field2 = $datagenerator->create_field($fieldrecord, $data2);

$record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
$record21 = $datagenerator->create_entry($data2, [$field2->field->id => 'value21'], 0, ['Cats']);
$tag = core_tag_tag::get_by_name(0, 'Cats');

// Admin can see everything.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);
$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertEmpty($res->prevpageurl);
$this->assertEmpty($res->nextpageurl);

// Create and enrol a user.
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->setUser($student);
core_tag_index_builder::reset_caches();

// User can search data records inside a course.
$coursecontext = context_course::instance($course1->id);
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertNotContains('value21', $res->content);

$this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');

core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
}

public function test_mod_data_get_tagged_records_course_groups() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course2 = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();

$groupa = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'name' => 'groupA'));
$groupb = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'name' => 'groupB'));

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
$field1 = $datagenerator->create_field($fieldrecord, $data1);
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course2->id));
$field2 = $datagenerator->create_field($fieldrecord, $data2);
set_coursemodule_groupmode($data2->cmid, SEPARATEGROUPS);

$record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'],
0, ['Cats', 'Dogs']);
$record21 = $datagenerator->create_entry($data2, [$field2->field->id => 'value21'],
$groupa->id, ['Cats']);
$record22 = $datagenerator->create_entry($data2, [$field2->field->id => 'value22'],
$groupb->id, ['Cats']);
$tag = core_tag_tag::get_by_name(0, 'Cats');

// Admin can see everything.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);
$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertContains('value22', $res->content);
$this->assertEmpty($res->prevpageurl);
$this->assertEmpty($res->nextpageurl);

// Create and enrol a user.
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
groups_add_member($groupa, $student);
$this->setUser($student);
core_tag_index_builder::reset_caches();

// User can search data records inside a course.
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertNotContains('value22', $res->content);

groups_add_member($groupb, $student);
core_tag_index_builder::reset_caches();
$res = mod_data_get_tagged_records($tag, false, 0, 0, 1, 0);

$this->assertContains('value11', $res->content);
$this->assertContains('value21', $res->content);
$this->assertContains('value22', $res->content);
}

/**
* Test check_updates_since callback.
*/
Expand Down
34 changes: 34 additions & 0 deletions mod/data/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,40 @@ public function test_advanced_search_sql_section() {
$this->assertEquals($this->approvedatarecordcount, count($recordids));
}

public function test_advanced_search_tags() {
$this->resetAfterTest();
$this->setAdminUser();

// Setup test data.
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$course1 = $this->getDataGenerator()->create_course();

$fieldrecord = new StdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';
$fieldrecord->titlefield = true;

$data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
$field1 = $datagenerator->create_field($fieldrecord, $data1);

$record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
$record12 = $datagenerator->create_entry($data1, [$field1->field->id => 'value12'], 0, ['Cats', 'mice']);
$record13 = $datagenerator->create_entry($data1, [$field1->field->id => 'value13'], 0, ['Bats']);

$searcharray = [];
$searcharray[DATA_TAGS] = new stdClass();
$searcharray[DATA_TAGS]->params = [];
$searcharray[DATA_TAGS]->rawtagnames = ['Cats'];
$searcharray[DATA_TAGS]->sql = '';

$recordids = data_get_all_recordids($data1->id);
$newrecordids = data_get_advance_search_ids($recordids, $searcharray, $data1->id);

$this->assertContains($record11, $newrecordids);
$this->assertContains($record12, $newrecordids);
$this->assertNotContains($record13, $newrecordids);
}

/**
* Indexing database entries contents.
*
Expand Down

0 comments on commit ee9a270

Please sign in to comment.