Skip to content

Commit

Permalink
MDL-41126 mod_data: Styling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 9, 2016
1 parent 20195fb commit d5bd76f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
51 changes: 24 additions & 27 deletions mod/data/tests/generator/lib.php
Expand Up @@ -15,8 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* mod_data data generator class
* Currently, the field types in the ignoredfieldtypes array aren't supported.
* Data generator class for mod_data.
*
* @package mod_data
* @category test
Expand All @@ -28,7 +27,8 @@


/**
* Database module data generator class
* Data generator class for mod_data.
*
* Currently, the field types in the ignoredfieldtypes array aren't supported.
*
* @package mod_data
Expand Down Expand Up @@ -86,7 +86,6 @@ public function create_instance($record = null, array $options = null) {
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.
Expand All @@ -96,8 +95,6 @@ public function create_instance($record = null, array $options = null) {
* @return data_field_{type}
*/
public function create_field($record = null, $data = null) {
global $DB;

$record = (array) $record;

if (in_array($record['type'], $this->ignoredfieldtypes)) {
Expand Down Expand Up @@ -212,7 +209,7 @@ public function create_entry($data, $contents) {

$recordid = data_add_record($data);

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

// Validating whether required field are filled.
foreach ($fields as $field) {
Expand All @@ -231,9 +228,9 @@ public function create_entry($data, $contents) {

$temp = explode('-', $contents[$fieldid], 3);

$values['field_'.$fieldid.'_day'] = $temp[0];
$values['field_'.$fieldid.'_month'] = $temp[1];
$values['field_'.$fieldid.'_year'] = $temp[2];
$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)) {
Expand All @@ -243,11 +240,11 @@ public function create_entry($data, $contents) {
} else if ($field->type === 'textarea') {
$values = array();

$values['field_'.$fieldid] = $contents[$fieldid];
$values['field_'.$fieldid.'_content1'] = 1;
$values['field_' . $fieldid] = $contents[$fieldid];
$values['field_' . $fieldid . '_content1'] = 1;

foreach ($values as $fieldname => $value) {
if ($field->notemptyfield ($value, $fieldname)) {
if ($field->notemptyfield($value, $fieldname)) {
continue 2;
}
}
Expand All @@ -256,20 +253,20 @@ public function create_entry($data, $contents) {

if (is_array($contents[$fieldid])) {
foreach ($contents[$fieldid] as $key => $value) {
$values['field_'.$fieldid.'_'.$key] = $value;
$values['field_' . $fieldid . '_' . $key] = $value;
}
} else {
$values['field_'.$fieldid.'_0'] = $contents[$fieldid];
$values['field_' . $fieldid . '_0'] = $contents[$fieldid];
}

foreach ($values as $fieldname => $value) {
if ($field->notemptyfield ($value, $fieldname)) {
if ($field->notemptyfield($value, $fieldname)) {
continue 2;
}
}

} else {
if ($field->notemptyfield ($contents[$fieldid], 'field_'.$fieldid.'_0')) {
if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) {
continue;
}
}
Expand All @@ -281,7 +278,7 @@ public function create_entry($data, $contents) {

foreach ($contents as $fieldid => $content) {

$field = $DB->get_record('data_fields', array( 'id' => $fieldid));
$field = $DB->get_record('data_fields', array('id' => $fieldid));
$field = data_get_field($field, $data);

if (in_array($field->field->type, $this->ignoredfieldtypes)) {
Expand All @@ -293,13 +290,13 @@ public function create_entry($data, $contents) {

$temp = explode('-', $content, 3);

$values['field_'.$fieldid.'_day'] = (int)trim($temp[0]);
$values['field_'.$fieldid.'_month'] = (int)trim($temp[1]);
$values['field_'.$fieldid.'_year'] = (int)trim($temp[2]);
$values['field_' . $fieldid . '_day'] = (int)trim($temp[0]);
$values['field_' . $fieldid . '_month'] = (int)trim($temp[1]);
$values['field_' . $fieldid . '_year'] = (int)trim($temp[2]);

// Year should be less than 2038, so it can be handled by 32 bit windows.
if ($values['field_'.$fieldid.'_year'] > 2038) {
throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond '.
if ($values['field_' . $fieldid . '_year'] > 2038) {
throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond ' .
'2038. Please use year less than 2038.');
}

Expand All @@ -313,8 +310,8 @@ public function create_entry($data, $contents) {
if ($field->type === 'textarea') {
$values = array();

$values['field_'.$fieldid] = $content;
$values['field_'.$fieldid.'_content1'] = 1;
$values['field_' . $fieldid] = $content;
$values['field_' . $fieldid . '_content1'] = 1;

foreach ($values as $fieldname => $value) {
$field->update_content($recordid, $value, $fieldname);
Expand All @@ -328,10 +325,10 @@ public function create_entry($data, $contents) {

if (is_array($content)) {
foreach ($content as $key => $value) {
$values['field_'.$fieldid.'_'.$key] = $value;
$values['field_' . $fieldid . '_' . $key] = $value;
}
} else {
$values['field_'.$fieldid.'_0'] = $content;
$values['field_' . $fieldid . '_0'] = $content;
}

foreach ($values as $fieldname => $value) {
Expand Down
36 changes: 15 additions & 21 deletions mod/data/tests/generator_test.php
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* PHPUnit data generator tests
* PHPUnit data generator tests.
*
* @package mod_data
* @category phpunit
Expand All @@ -27,7 +27,7 @@


/**
* PHPUnit data generator testcase
* PHPUnit data generator testcase.
*
* @package mod_data
* @category phpunit
Expand Down Expand Up @@ -70,10 +70,8 @@ public function test_generator() {
$this->assertEquals(100, $gitem->grademax);
$this->assertEquals(0, $gitem->grademin);
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);

}


public function test_create_field() {
global $DB;

Expand All @@ -100,37 +98,36 @@ public function test_create_field() {
$context = context_module::instance($cm->id);
$this->assertEquals($data->cmid, $context->instanceid);

$fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
$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;
$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});
$this->assertInstanceOf('data_field_' . $fieldtype, ${$fieldname});
$count++;
}

$this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
$this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array('dataid' => $data->id)));

$addtemplate = $DB->get_record('data', array( 'id' => $data->id ), 'addtemplate');
$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);
$fieldname = 'field-' . $i;
$this->assertTrue(strpos($addtemplate, '[[' . $fieldname . ']]') >= 0);
}
}


public function test_create_entry() {
global $DB;

Expand All @@ -157,25 +154,25 @@ public function test_create_entry() {
$context = context_module::instance($cm->id);
$this->assertEquals($data->cmid, $context->instanceid);

$fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
$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;
$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});
$this->assertInstanceOf('data_field_' . $fieldtype, ${$fieldname});
$count++;
}

$this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array( 'dataid' => $data->id )));
$this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array('dataid' => $data->id)));

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

Expand All @@ -197,10 +194,7 @@ public function test_create_entry() {

$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(1, $DB->count_records('data_records', array('dataid' => $data->id)));
$this->assertEquals(count($contents), $DB->count_records('data_content', array('recordid' => $datarecordid)));

}

}
}

0 comments on commit d5bd76f

Please sign in to comment.