MDL-73971 phpunit: Move more tests to use correct names and namespaces
Applied the following changes to various testcase classes: - Namespaced with component[\level2-API] - Moved to level2-API subdirectory when required. - Fixed incorrect use statements with leading backslash. - Remove file phpdoc block - Remove MOODLE_INTERNAL if not needed. - Changed code to point to global scope when needed. - Fix some relative paths and comments here and there. - All them passing individually. - Complete runs passing too. Special mention to: - Some fixtures, initially defined in the test files have been moved to new files in fixtures subdirectory, leaving the unit test files clearer: - moodle2_course_format_test.php - Rename wrong named test: - baseoptiogroup_test = baseoptigroup_test
This commit is contained in:
@@ -14,25 +14,24 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core_customfield;
|
||||
|
||||
use core_customfield_generator;
|
||||
use customfield_checkbox;
|
||||
use customfield_date;
|
||||
use customfield_select;
|
||||
use customfield_text;
|
||||
use customfield_textarea;
|
||||
|
||||
/**
|
||||
* Tests for class data_controller.
|
||||
* Functional test for class data_controller.
|
||||
*
|
||||
* @package core_customfield
|
||||
* @category test
|
||||
* @copyright 2018 Toni Barbera <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core_customfield\data_controller;
|
||||
|
||||
/**
|
||||
* Functional test for class data_controller.
|
||||
* @copyright 2018 Toni Barbera <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
class data_controller_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* Get generator.
|
||||
@@ -55,7 +54,7 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
$category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
|
||||
|
||||
// Add fields to this category.
|
||||
$fielddata = new stdClass();
|
||||
$fielddata = new \stdClass();
|
||||
$fielddata->categoryid = $category0->get('id');
|
||||
$fielddata->configdata = "{\"required\":\"0\",\"uniquevalues\":\"0\",\"locked\":\"0\",\"visibility\":\"0\",
|
||||
\"defaultvalue\":\"\",\"displaysize\":0,\"maxlength\":0,\"ispassword\":\"0\",
|
||||
@@ -72,7 +71,7 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
$fielddata->type = 'textarea';
|
||||
$field4 = $this->get_generator()->create_field($fielddata);
|
||||
|
||||
$params = ['instanceid' => $course->id, 'contextid' => context_course::instance($course->id)->id];
|
||||
$params = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
|
||||
|
||||
// Generate new data_controller records for these fields, specifying field controller or fieldid or both.
|
||||
$data0 = data_controller::create(0, (object)$params, $field0);
|
||||
@@ -125,7 +124,7 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
$field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$data = data_controller::create(0, (object)['instanceid' => $course->id,
|
||||
'contextid' => context_course::instance($course->id)->id], $field);
|
||||
'contextid' => \context_course::instance($course->id)->id], $field);
|
||||
$data->save();
|
||||
|
||||
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data->get('id')], '*', MUST_EXIST);
|
||||
@@ -143,29 +142,29 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
try {
|
||||
data_controller::create($datarecord->id + 1);
|
||||
$this->fail('Expected exception');
|
||||
} catch (dml_missing_record_exception $e) {
|
||||
} catch (\dml_missing_record_exception $e) {
|
||||
$this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
|
||||
$this->assertEquals(dml_missing_record_exception::class, get_class($e));
|
||||
$this->assertEquals(\dml_missing_record_exception::class, get_class($e));
|
||||
}
|
||||
|
||||
// Missing field id.
|
||||
try {
|
||||
data_controller::create(0, (object)['instanceid' => $course->id]);
|
||||
$this->fail('Expected exception');
|
||||
} catch (coding_exception $e) {
|
||||
} catch (\coding_exception $e) {
|
||||
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
|
||||
'initialise data_controller - unknown field', $e->getMessage());
|
||||
$this->assertEquals(coding_exception::class, get_class($e));
|
||||
$this->assertEquals(\coding_exception::class, get_class($e));
|
||||
}
|
||||
|
||||
// Mismatching field id.
|
||||
try {
|
||||
data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
|
||||
$this->fail('Expected exception');
|
||||
} catch (coding_exception $e) {
|
||||
} catch (\coding_exception $e) {
|
||||
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
|
||||
'does not match field from the parameter', $e->getMessage());
|
||||
$this->assertEquals(coding_exception::class, get_class($e));
|
||||
$this->assertEquals(\coding_exception::class, get_class($e));
|
||||
}
|
||||
|
||||
// Nonexisting class.
|
||||
@@ -173,9 +172,9 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
|
||||
$field->set('type', 'invalid');
|
||||
data_controller::create(0, (object)['instanceid' => $course->id], $field);
|
||||
$this->fail('Expected exception');
|
||||
} catch (moodle_exception $e) {
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertEquals('Field type invalid not found', $e->getMessage());
|
||||
$this->assertEquals(moodle_exception::class, get_class($e));
|
||||
$this->assertEquals(\moodle_exception::class, get_class($e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user