MDL-57455 mod_data: Added PHPUnit tests
authorAndrew Hancox <andrewdchancox@googlemail.com>
Thu, 14 Sep 2017 10:17:05 +0000 (18:17 +0800)
committerMark Nelson <markn@moodle.com>
Thu, 12 Oct 2017 08:59:15 +0000 (16:59 +0800)
mod/data/tests/generator/lib.php
mod/data/tests/generator_test.php
mod/data/tests/lib_test.php
mod/data/tests/search_test.php

index 3fe9ef5..02a4809 100644 (file)
@@ -209,15 +209,24 @@ class mod_data_generator extends testing_module_generator {
      *
      * @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.
@@ -309,6 +318,12 @@ class mod_data_generator extends testing_module_generator {
             }
         }
 
+        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;
     }
 }
index cb199d8..bb4945e 100644 (file)
@@ -198,8 +198,10 @@ class mod_data_generator_testcase extends advanced_testcase {
             $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)));
@@ -229,5 +231,7 @@ class mod_data_generator_testcase extends advanced_testcase {
         $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)));
     }
 }
index ca6e0be..351879b 100644 (file)
@@ -976,6 +976,271 @@ class mod_data_lib_testcase extends advanced_testcase {
         $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.
      */
index 105571f..9228233 100644 (file)
@@ -257,6 +257,40 @@ class mod_data_search_test extends advanced_testcase {
         $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.
      *