MDL-41126 mod_data: added field and entry generator
authorDevang Gaur <devang.only@gmail.com>
Thu, 4 Aug 2016 22:38:28 +0000 (04:08 +0530)
committerDevang Gaur <devang.only@gmail.com>
Thu, 4 Aug 2016 22:38:50 +0000 (04:08 +0530)
mod/data/tests/generator/lib.php
mod/data/tests/generator_test.php

index 9098904..b02a97c 100644 (file)
@@ -15,7 +15,8 @@
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 /**
- * mod_data data generator
+ * mod_data data generator class
+ * Currently, the field types in the ignoredfieldtypes array aren't supported.
  *
  * @package    mod_data
  * @category   test
@@ -28,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
 
 /**
  * Database module data generator class
+ * Currently, the field types in the ignoredfieldtypes array aren't supported.
  *
  * @package    mod_data
  * @category   test
@@ -36,6 +38,41 @@ defined('MOODLE_INTERNAL') || die();
  */
 class mod_data_generator extends testing_module_generator {
 
+    /**
+     * @var int keep track of how many database fields have been created.
+     */
+    protected $databasefieldcount = 0;
+
+    /**
+     * @var int keep track of how many database records have been created.
+     */
+    protected $databaserecordcount = 0;
+
+    /**
+     * @var The field types which not handled by the generator as of now.
+     */
+    protected $ignoredfieldtypes = array('latlong', 'file', 'picture');
+
+
+    /**
+     * To be called from data reset code only,
+     * do not use in tests.
+     * @return void
+     */
+    public function reset() {
+        $this->databasefieldcount = 0;
+        $this->databaserecordcount = 0;
+
+        parent::reset();
+    }
+
+    /**
+     * Creates a mod_data instance
+     *
+     * @param array $record
+     * @param array $options
+     * @return StdClass
+     */
     public function create_instance($record = null, array $options = null) {
         $record = (object)(array)$record;
 
@@ -48,4 +85,259 @@ class mod_data_generator extends testing_module_generator {
 
         return parent::create_instance($record, (array)$options);
     }
+
+
+    /**
+     * Creates a field for a mod_data instance.
+     * Currently, the field types in the ignoredfieldtypes array aren't supported.
+     *
+     * @param StdClass $record
+     * @param mod_data $data
+     * @return data_field_{type}
+     */
+    public function create_field($record = null, $data = null) {
+        global $DB;
+
+        $record = (array) $record;
+
+        if (in_array($record['type'], $this->ignoredfieldtypes)) {
+            throw new coding_exception('$record\'s type value must not be same as values in ignoredfieldtypes
+                    in phpunit_util::create_field()');
+            return false;
+        }
+
+        $this->databasefieldcount++;
+
+        if (!isset($data->course)) {
+            throw new coding_exception('course must be present in phpunit_util::create_field() $data');
+        }
+
+        if (!isset($data->id)) {
+            throw new coding_exception('dataid must be present in phpunit_util::create_field() $data');
+        } else {
+            $record['dataid'] = $data->id;
+        }
+
+        if (!isset($record['type'])) {
+            throw new coding_exception('type must be present in phpunit_util::create_field() $record');
+        }
+
+        if (!isset($record['required'])) {
+            $record['required'] = 0;
+        }
+
+        if (!isset($record['name'])) {
+            $record['name'] = "testField - " . $this->databasefieldcount;
+        }
+
+        if (!isset($record['description'])) {
+            $record['description'] = " This is testField - " . $this->databasefieldcount;
+        }
+
+        if (!isset($record['param1'])) {
+            if (in_array($record['type'], array('checkbox', 'menu', 'multimenu', 'radiobutton'))) {
+                $record['param1'] = implode("\n", array('one', 'two', 'three', 'four'));
+            } else if (($record['type'] === 'text') || ($record['type'] === 'url')) {
+                $record['param1'] = 1;
+            } else {
+                $record['param1'] = '';
+            }
+        }
+
+        if (!isset($record['param2'])) {
+
+            if ($record['type'] === 'textarea') {
+                $record['param2'] = 60;
+            } else {
+                $record['param2'] = '';
+            }
+        }
+
+        if (!isset($record['param3'])) {
+
+            if (($record['type'] === 'textarea')) {
+                $record['param3'] = 35;
+            } else {
+                $record['param3'] = '';
+            }
+        }
+
+        if (!isset($record['param4'])) {
+
+            if (($record['type'] === 'textarea')) {
+                $record['param4'] = 1;
+            }
+        }
+
+        if (!isset($record['param5'])) {
+            if (($record['type'] === 'textarea')) {
+                $record['param5'] = 0;
+            }
+        }
+
+        $record = (object) $record;
+
+        $field = data_get_field($record, $data);
+        $field->insert_field();
+
+        data_generate_default_template($data, 'addtemplate', 0, false, true);
+
+        return $field;
+    }
+
+    /**
+     * Creates a field for a mod_data instance.
+     * Currently, the field types in the ignoredfieldtypes array aren't supported.
+     * The developers using the generator must adhere to the following format :
+     *
+     *   Syntax : $contents[ fieldid ] = fieldvalue
+     *   $contents['checkbox'] = array('val1', 'val2', 'val3' .....)
+     *   $contents['data'] = 'dd-mm-yyyy'
+     *   $contents['menu'] = 'value';
+     *   $contents['multimenu'] =  array('val1', 'val2', 'val3' .....)
+     *   $contents['number'] = 'numeric value'
+     *   $contents['radiobuton'] = 'value'
+     *   $contents['text'] = 'text'
+     *   $contents['textarea'] = 'text'
+     *   $contents['url'] = 'example.url' or array('example.url', 'urlname')
+     *
+     * @param mod_data $data
+     * @param array $contents
+     * @return data_field_{type}
+     */
+    public function create_entry($data, $contents) {
+        global $DB;
+
+        $this->databaserecordcount++;
+
+        $recordid = data_add_record($data);
+
+        $fields = $DB->get_records('data_fields', array( 'dataid' => $data->id));
+
+        // Validating whether required field are filled.
+        foreach ($fields as $field) {
+            $fieldhascontent = false;
+
+            if (in_array($field->type, $this->ignoredfieldtypes)) {
+                continue;
+            }
+
+            $field = data_get_field($field, $data);
+
+            $fieldid = $field->field->id;
+
+            if ($field->type === 'date') {
+                $values = array();
+
+                $temp = explode('-', $contents[$fieldid], 3);
+
+                $values['field_'.$fieldid.'_day'] = $temp[0];
+                $values['field_'.$fieldid.'_month'] = $temp[1];
+                $values['field_'.$fieldid.'_year'] = $temp[2];
+
+                foreach ($values as $fieldname => $value) {
+                    if ($field->notemptyfield($value, $fieldname)) {
+                        continue 2;
+                    }
+                }
+            } else if ($field->type === 'textarea') {
+                $values = array();
+
+                $values['field_'.$fieldid] = $contents[$fieldid];
+                $values['field_'.$fieldid.'_content1'] = 1;
+
+                foreach ($values as $fieldname => $value) {
+                    if ($field->notemptyfield ($value, $fieldname)) {
+                        continue 2;
+                    }
+                }
+            } else if ($field->type === 'url') {
+                $values = array();
+
+                if (is_array($contents[$fieldid])) {
+                    foreach ($contents[$fieldid] as $key => $value) {
+                        $values['field_'.$fieldid.'_'.$key] = $value;
+                    }
+                } else {
+                    $values['field_'.$fieldid.'_0'] = $contents[$fieldid];
+                }
+
+                foreach ($values as $fieldname => $value) {
+                    if ($field->notemptyfield ($value, $fieldname)) {
+                        continue 2;
+                    }
+                }
+
+            } else {
+                if ($field->notemptyfield ($contents[$fieldid], 'field_'.$fieldid.'_0')) {
+                    continue;
+                }
+            }
+
+            if ($field->field->required && !$fieldhascontent) {
+                return false;
+            }
+        }
+
+        foreach ($contents as $fieldid => $content) {
+
+            $field = $DB->get_record('data_fields', array( 'id' => $fieldid));
+            $field = data_get_field($field, $data);
+
+            if (in_array($field->field->type, $this->ignoredfieldtypes)) {
+                continue;
+            }
+
+            if ($field->type === 'date') {
+                $values = array();
+
+                $temp = explode('-', $content, 3);
+
+                $values['field_'.$fieldid.'_day'] = $temp[0];
+                $values['field_'.$fieldid.'_month'] = $temp[1];
+                $values['field_'.$fieldid.'_year'] = $temp[2];
+
+                foreach ($values as $fieldname => $value) {
+                    $field->update_content($recordid, (string)(int)trim($value), $fieldname);
+                }
+
+                continue;
+            }
+
+            if ($field->type === 'textarea') {
+                $values = array();
+
+                $values['field_'.$fieldid] = $content;
+                $values['field_'.$fieldid.'_content1'] = 1;
+
+                foreach ($values as $fieldname => $value) {
+                    $field->update_content($recordid, $value, $fieldname);
+                }
+
+                continue;
+            }
+
+            if ($field->type === 'url') {
+                $values = array();
+
+                if (is_array($content)) {
+                    foreach ($content as $key => $value) {
+                        $values['field_'.$fieldid.'_'.$key] = $value;
+                    }
+                } else {
+                    $values['field_'.$fieldid.'_0'] = $content;
+                }
+
+                foreach ($values as $fieldname => $value) {
+                    $field->update_content($recordid, $value, $fieldname);
+                }
+
+                continue;
+            }
+
+            $field->update_content($recordid, $contents[$fieldid]);
+        }
+
+        return $recordid;
+    }
 }
index a10d11a..113a114 100644 (file)
@@ -49,9 +49,9 @@ class mod_data_generator_testcase extends advanced_testcase {
         $this->assertInstanceOf('mod_data_generator', $generator);
         $this->assertEquals('data', $generator->get_modulename());
 
-        $generator->create_instance(array('course'=>$course->id));
-        $generator->create_instance(array('course'=>$course->id));
-        $data = $generator->create_instance(array('course'=>$course->id));
+        $generator->create_instance(array('course' => $course->id));
+        $generator->create_instance(array('course' => $course->id));
+        $data = $generator->create_instance(array('course' => $course->id));
         $this->assertEquals(3, $DB->count_records('data'));
 
         $cm = get_coursemodule_from_instance('data', $data->id);
@@ -63,12 +63,144 @@ class mod_data_generator_testcase extends advanced_testcase {
         $this->assertEquals($data->cmid, $context->instanceid);
 
         // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
-        $data = $generator->create_instance(array('course'=>$course->id, 'assessed'=>1, 'scale'=>100));
-        $gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'data', 'iteminstance'=>$data->id));
+        $data = $generator->create_instance(array('course' => $course->id, 'assessed' => 1, 'scale' => 100));
+        $gitem = $DB->get_record('grade_items', array('courseid' => $course->id, 'itemtype' => 'mod',
+                'itemmodule' => 'data', 'iteminstance' => $data->id));
         $this->assertNotEmpty($gitem);
         $this->assertEquals(100, $gitem->grademax);
         $this->assertEquals(0, $gitem->grademin);
         $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
 
     }
-}
+
+
+    public function test_create_field() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $this->setAdminUser();
+        $this->assertEquals(0, $DB->count_records('data'));
+
+        $course = $this->getDataGenerator()->create_course();
+
+        /** @var mod_data_generator $generator */
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
+        $this->assertInstanceOf('mod_data_generator', $generator);
+        $this->assertEquals('data', $generator->get_modulename());
+
+        $data = $generator->create_instance(array('course' => $course->id));
+        $this->assertEquals(1, $DB->count_records('data'));
+
+        $cm = get_coursemodule_from_instance('data', $data->id);
+        $this->assertEquals($data->id, $cm->instance);
+        $this->assertEquals('data', $cm->modname);
+        $this->assertEquals($course->id, $cm->course);
+
+        $context = context_module::instance($cm->id);
+        $this->assertEquals($data->cmid, $context->instanceid);
+
+        $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
+
+        $count = 1;
+
+        // Creating test Fields with default parameter values.
+        foreach ($fieldtypes as $fieldtype) {
+
+            // Creating variables dynamically.
+            $fieldname = 'field-'.$count;
+            $record = new StdClass();
+            $record->name = $fieldname;
+            $record->type = $fieldtype;
+
+            ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
+
+            $this->assertInstanceOf('data_field_'.$fieldtype , ${$fieldname});
+            $count++;
+        }
+
+        $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
+
+        $addtemplate = $DB->get_record('data', array( 'id' => $data->id ), 'addtemplate');
+        $addtemplate = $addtemplate->addtemplate;
+
+        for ($i = 1; $i < $count; $i++) {
+            $fieldname = 'field-'.$i;
+            $this->assertTrue(strpos($addtemplate, '[['.$fieldname.']]') >= 0);
+        }
+    }
+
+
+    public function test_create_entry() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $this->setAdminUser();
+        $this->assertEquals(0, $DB->count_records('data'));
+
+        $course = $this->getDataGenerator()->create_course();
+
+        /** @var mod_data_generator $generator */
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
+        $this->assertInstanceOf('mod_data_generator', $generator);
+        $this->assertEquals('data', $generator->get_modulename());
+
+        $data = $generator->create_instance(array('course' => $course->id));
+        $this->assertEquals(1, $DB->count_records('data'));
+
+        $cm = get_coursemodule_from_instance('data', $data->id);
+        $this->assertEquals($data->id, $cm->instance);
+        $this->assertEquals('data', $cm->modname);
+        $this->assertEquals($course->id, $cm->course);
+
+        $context = context_module::instance($cm->id);
+        $this->assertEquals($data->cmid, $context->instanceid);
+
+        $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
+
+        $count = 1;
+
+        // Creating test Fields with default parameter values.
+        foreach ($fieldtypes as $fieldtype) {
+
+            // Creating variables dynamically.
+            $fieldname = 'field-'.$count;
+            $record = new StdClass();
+            $record->name = $fieldname;
+            $record->type = $fieldtype;
+
+            ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
+            $this->assertInstanceOf('data_field_'.$fieldtype , ${$fieldname});
+            $count++;
+        }
+
+        $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
+
+        $fields = $DB->get_records('data_fields', array('dataid' => $data->id), 'id');
+
+        $contents = array();
+        $contents[] = array('one', 'two', 'three', 'four');
+        $contents[] = '01-01-2100';
+        $contents[] = 'one';
+        $contents[] = array('one', 'two', 'three', 'four');
+        $contents[] = '12345';
+        $contents[] = 'one';
+        $contents[] = 'text for testing';
+        $contents[] = '<p>text area testing<br /></p>';
+        $contents[] = array('example.url', 'sampleurl');
+        $count = 0;
+        $fieldcontents = array();
+        foreach ($fields as $fieldrecord) {
+            $fieldcontents[$fieldrecord->id] = $contents[$count++];
+        }
+
+        $datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data, $fieldcontents);
+
+        $this->assertEquals(1, $DB->count_records('data_records', array( 'dataid' => $data->id )));
+
+        $this->assertEquals(count($contents), $DB->count_records('data_content', array('recordid' => $datarecordid)));
+
+    }
+
+}
\ No newline at end of file