Skip to content

Commit

Permalink
MDL-57898 core_customfield: Custom fields API
Browse files Browse the repository at this point in the history
This commit is part of work on Custom fields API,
to minimize commit history in moodle core the work of a team of developers was split
into several commits with different authors but the authorship of individual
lines of code may be different from the commit author.
  • Loading branch information
marinaglancy committed Jan 18, 2019
1 parent 01c1095 commit 0e367a1
Show file tree
Hide file tree
Showing 14 changed files with 1,486 additions and 1 deletion.
84 changes: 84 additions & 0 deletions customfield/classes/privacy/customfield_provider.php
@@ -0,0 +1,84 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains interface customfield_provider
*
* @package core_customfield
* @copyright 2018 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_customfield\privacy;

use core_customfield\data_controller;

defined('MOODLE_INTERNAL') || die();

/**
* Interface customfield_provider, all customfield plugins need to implement it
*
* @package core_customfield
* @copyright 2018 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface customfield_provider extends
\core_privacy\local\request\plugin\subplugin_provider,

// The customfield plugins do not need to do anything themselves for the shared_userlist.
// This is all handled by the component core_customfield.
\core_privacy\local\request\shared_userlist_provider
{

/**
* Preprocesses data object that is going to be exported
*
* Minimum implementation:
* writer::with_context($data->get_context())->export_data($subcontext, $exportdata);
*
* @param data_controller $data
* @param \stdClass $exportdata generated object to be exported
* @param array $subcontext subcontext to use when exporting
* @return mixed
*/
public static function export_customfield_data(data_controller $data, \stdClass $exportdata, array $subcontext);

/**
* Allows plugins to delete everything they store related to the data (usually files)
*
* If plugin does not store any related files or other information, implement as an empty function
*
* @param string $dataidstest select query for data id (note that it may also return data for other field types)
* @param array $params named parameters for the select query
* @param array $contextids list of affected data contexts
* @return mixed
*/
public static function before_delete_data(string $dataidstest, array $params, array $contextids);

/**
* Allows plugins to delete everything they store related to the field configuration (usually files)
*
* The implementation should not delete data or anything related to the data, since "before_delete_data" is
* invoked separately.
*
* If plugin does not store any related files or other information, implement as an empty function
*
* @param string $fieldidstest select query for field id (note that it may also return fields of other types)
* @param array $params named parameters for the select query
* @param int[] $contextids list of affected configuration contexts
*/
public static function before_delete_fields(string $fieldidstest, array $params, array $contextids);
}
177 changes: 177 additions & 0 deletions customfield/field/checkbox/tests/plugin_test.php
@@ -0,0 +1,177 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tests for class customfield_checkbox
*
* @package customfield_checkbox
* @copyright 2019 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

use customfield_checkbox\field_controller;
use customfield_checkbox\data_controller;

/**
* Functional test for customfield_checkbox
*
* @package customfield_checkbox
* @copyright 2019 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class customfield_checkbox_plugin_testcase extends advanced_testcase {

/** @var stdClass[] */
private $courses = [];
/** @var \core_customfield\category_controller */
private $cfcat;
/** @var \core_customfield\field_controller[] */
private $cfields;
/** @var \core_customfield\data_controller[] */
private $cfdata;

/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();

$this->cfcat = $this->get_generator()->create_category();

$this->cfields[1] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'checkbox']);
$this->cfields[2] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'checkbox',
'configdata' => ['required' => 1]]);
$this->cfields[3] = $this->get_generator()->create_field(
['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'checkbox',
'configdata' => ['checkbydefault' => 1]]);

$this->courses[1] = $this->getDataGenerator()->create_course();
$this->courses[2] = $this->getDataGenerator()->create_course();
$this->courses[3] = $this->getDataGenerator()->create_course();

$this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1);
$this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1);

$this->setUser($this->getDataGenerator()->create_user());
}

/**
* Get generator
* @return core_customfield_generator
*/
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}

/**
* Test for initialising field and data controllers
*/
public function test_initialise() {
$f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
$this->assertTrue($f instanceof field_controller);

$f = \core_customfield\field_controller::create(0, (object)['type' => 'checkbox'], $this->cfcat);
$this->assertTrue($f instanceof field_controller);

$d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
$this->assertTrue($d instanceof data_controller);

$d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
$this->assertTrue($d instanceof data_controller);
}

/**
* Test for configuration form functions
*
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');

\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[1], $data);

// Try submitting with 'unique values' checked.
$submitdata['configdata']['uniquevalues'] = 1;
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$this->assertFalse($form->is_validated());
}

/**
* Test for instance form functions
*/
public function test_instance_form() {
global $CFG;
require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
$this->setAdminUser();
$handler = $this->cfcat->get_handler();

// First try to submit without required field.
$submitdata = (array)$this->courses[1];
core_customfield_test_instance_form::mock_submit($submitdata, []);
$form = new core_customfield_test_instance_form('POST',
['handler' => $handler, 'instance' => $this->courses[1]]);
$this->assertFalse($form->is_validated());

// Now with required field.
$submitdata['customfield_myfield2'] = 1;
core_customfield_test_instance_form::mock_submit($submitdata, []);
$form = new core_customfield_test_instance_form('POST',
['handler' => $handler, 'instance' => $this->courses[1]]);
$this->assertTrue($form->is_validated());

$data = $form->get_data();
$this->assertNotEmpty($data->customfield_myfield1);
$this->assertNotEmpty($data->customfield_myfield2);
$handler->instance_form_save($data);
}

/**
* Test for data_controller::get_value and export_value
*/
public function test_get_export_value() {
$this->assertEquals(1, $this->cfdata[1]->get_value());
$this->assertEquals('Yes', $this->cfdata[1]->export_value());

// Field without data.
$d = core_customfield\data_controller::create(0, null, $this->cfields[2]);
$this->assertEquals(0, $d->get_value());
$this->assertEquals('No', $d->export_value());

// Field without data that is checked by default.
$d = core_customfield\data_controller::create(0, null, $this->cfields[3]);
$this->assertEquals(1, $d->get_value());
$this->assertEquals('Yes', $d->export_value());
}

/**
* Deleting fields and data
*/
public function test_delete() {
$this->cfcat->get_handler()->delete_all();
}
}

0 comments on commit 0e367a1

Please sign in to comment.