Skip to content

Commit

Permalink
MDL-75111 phpunit: Move tests to use correct names and ns (take#4)
Browse files Browse the repository at this point in the history
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:

- The following task tests have been moved within the level2 directory:
  - \core\adhoc_task_test => \core\task\adhoc_task_test
  - \core\scheduled_task_test => \core\task\scheduled_task_test
  - \core\calendar_cron_task_test => \core\task\calendar_cron_task_test
  - \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test
  - \core\task_database_logger_test => \core\task\database_logger_test
  - \core\task_logging_test => \core\task\logging_test

- The following event tests have been moved within level2 directory:
  - \core\event_context_locked_test => \core\event\context_locked_test
  - \core\event_deprecated_test => \core\event\deprecated_test
  - \core\event_grade_deleted_test => \core\event\grade_deleted_test
  - \core\event_profile_field_test => \core\event\profile_field_test
  - \core\event_unknown_logged_test => \core\event\unknown_logged_test
  - \core\event_user_graded_test => \core\event\user_graded_test
  - \core\event_user_password_updated_test => \core\event\user_password_updated_test

- The following output tests have been moved within level2 directory:
  - \core\mustache_template_finder_test => \core\output\mustache_template_finder_test
  - \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test
  - \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test

- The following tests have been moved to their correct tests directories:
  - lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php

- All the classes and tests under lib/filebrowser and lib/filestorage
  belong to core, not to core_files. Some day we should move
  them to their correct subsystem.
- All the classes and tests under lib/grade belong to core, not
  to core_grades. Some day we should move them to their correct
  subsystem.
- The core_grades_external class and its \core\grades_external_test
  unit test should belong to the grades subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The core_grading_external class and its \core\grading_external_test
  unit test should belong to the grading subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The \core\message\message and \core\message\inbound (may be others)
  classes, and their associated tests should go to the core_message
  subsystem.
- The core_user class, and its associated tests should go to the
  core_user subsystem.
- The \core\update namespace is plain wrong (update is not valid API)
  and needs action 1) create it or 2) move elsewhere.
  • Loading branch information
stronk7 committed Aug 26, 2022
1 parent 376e57d commit 61cedbd
Show file tree
Hide file tree
Showing 125 changed files with 2,435 additions and 2,719 deletions.
19 changes: 5 additions & 14 deletions analytics/tests/analysis_test.php
Expand Up @@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the analysis class.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
namespace core_analytics;

/**
* Unit tests for the analysis class.
Expand All @@ -33,18 +23,19 @@
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_analysis_testcase extends advanced_testcase {
class analysis_test extends \advanced_testcase {

/**
* Test fill_firstanalyses_cache.
* @return null
*/
public function test_fill_firstanalyses_cache() {
require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
$this->resetAfterTest();

$modelid = 1;

$params = ['startdate' => (new DateTimeImmutable('-5 seconds'))->getTimestamp()];
$params = ['startdate' => (new \DateTimeImmutable('-5 seconds'))->getTimestamp()];
$course1 = $this->getDataGenerator()->create_course($params);
$course2 = $this->getDataGenerator()->create_course($params);
$analysable1 = new \core_analytics\course($course1);
Expand Down Expand Up @@ -72,7 +63,7 @@ public function test_fill_firstanalyses_cache() {
$this->assertEquals($earliest, $firstanalyses[$modelid . '_' . $course1->id]);

// Upcoming periodic time-splitting methods can read and process the cached data.
$seconds = new test_timesplitting_upcoming_seconds();
$seconds = new \test_timesplitting_upcoming_seconds();
$seconds->set_modelid($modelid);
$seconds->set_analysable($analysable1);

Expand Down
21 changes: 6 additions & 15 deletions analytics/tests/calculation_info_test.php
Expand Up @@ -14,18 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the calculation info cache.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once(__DIR__ . '/fixtures/test_indicator_max.php');
require_once(__DIR__ . '/fixtures/test_indicator_min.php');
namespace core_analytics;

/**
* Unit tests for the calculation info cache.
Expand All @@ -34,7 +23,7 @@
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_calculation_info_testcase extends advanced_testcase {
class calculation_info_test extends \advanced_testcase {

/**
* test_calculation_info description
Expand All @@ -47,12 +36,14 @@ class analytics_calculation_info_testcase extends advanced_testcase {
* @return null
*/
public function test_calculation_info_add_pull($info1, $info2, $info3, $info4) {
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
require_once(__DIR__ . '/fixtures/test_indicator_min.php');
$this->resetAfterTest();

$atimesplitting = new \core\analytics\time_splitting\quarters();

$indicator1 = new test_indicator_min();
$indicator2 = new test_indicator_max();
$indicator1 = new \test_indicator_min();
$indicator2 = new \test_indicator_max();

$calculationinfo = new \core_analytics\calculation_info();
$calculationinfo->add_shared(111, [111 => $info1]);
Expand Down
12 changes: 2 additions & 10 deletions analytics/tests/dataset_manager_test.php
Expand Up @@ -14,15 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the dataset manager.
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

/**
* Unit tests for the dataset manager.
Expand All @@ -31,7 +23,7 @@
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dataset_manager_testcase extends advanced_testcase {
class dataset_manager_test extends \advanced_testcase {

/**
* setUp
Expand Down
12 changes: 3 additions & 9 deletions analytics/tests/indicator_test.php
Expand Up @@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the indicator API.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;

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

Expand All @@ -35,7 +29,7 @@
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_indicator_testcase extends advanced_testcase {
class indicator_test extends \advanced_testcase {

/**
* test_validate_calculated_value
Expand Down Expand Up @@ -79,7 +73,7 @@ public function test_validate_calculated_value_exceptions($indicatorclass, $will
->onlyMethods(['calculate_sample'])
->getMock();
$indicatormock->method('calculate_sample')->willReturn($willreturn);
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere');

}
Expand Down
18 changes: 10 additions & 8 deletions analytics/tests/model_test.php
Expand Up @@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_analytics;

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

require_once(__DIR__ . '/fixtures/test_indicator_max.php');
Expand All @@ -39,7 +41,7 @@
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_model_testcase extends advanced_testcase {
class model_test extends \advanced_testcase {

public function setUp(): void {

Expand Down Expand Up @@ -183,7 +185,7 @@ public function test_clear_static() {
global $DB;
$this->resetAfterTest();

$statictarget = new test_static_target_shortname();
$statictarget = new \test_static_target_shortname();
$indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
$model = \core_analytics\model::create($statictarget, $indicators, '\core\analytics\time_splitting\quarters');
$modelobj = $model->get_model_obj();
Expand Down Expand Up @@ -301,11 +303,11 @@ public function test_model_timelimit() {
$courses[$analysable->get_id()] = $course;
}

$target = new test_target_course_level_shortname();
$target = new \test_target_course_level_shortname();
$analyser = new \core\analytics\analyser\courses(1, $target, [], [], []);

$result = new \core_analytics\local\analysis\result_array(1, false, []);
$analysis = new test_analysis($analyser, false, $result);
$analysis = new \test_analysis($analyser, false, $result);

// Each analysable element takes 0.5 secs minimum (test_analysis), so the max (and likely) number of analysable
// elements that will be processed is 2.
Expand Down Expand Up @@ -342,7 +344,7 @@ public function test_model_timelimit() {

// Training and prediction data do not get mixed.
$result = new \core_analytics\local\analysis\result_array(1, false, []);
$analysis = new test_analysis($analyser, false, $result);
$analysis = new \test_analysis($analyser, false, $result);
$analysis->run();
$params = array('modelid' => 1, 'action' => 'training');
$this->assertLessThanOrEqual(2, $DB->count_records('analytics_used_analysables', $params));
Expand Down Expand Up @@ -402,7 +404,7 @@ public function test_can_export_configuration() {
$this->model->update(true, [], false);
$this->assertFalse($this->model->can_export_configuration());

$statictarget = new test_static_target_shortname();
$statictarget = new \test_static_target_shortname();
$indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
$model = \core_analytics\model::create($statictarget, $indicators, '\\core\\analytics\\time_splitting\\quarters');
$this->assertFalse($model->can_export_configuration());
Expand All @@ -418,7 +420,7 @@ public function test_export_config() {

$modelconfig = new \core_analytics\model_config($this->model);

$method = new ReflectionMethod('\\core_analytics\\model_config', 'export_model_data');
$method = new \ReflectionMethod('\\core_analytics\\model_config', 'export_model_data');
$method->setAccessible(true);

$modeldata = $method->invoke($modelconfig);
Expand Down Expand Up @@ -566,7 +568,7 @@ public function test_get_samples() {
private function add_fake_log() {
global $DB, $USER;

$log = new stdClass();
$log = new \stdClass();
$log->modelid = $this->modelobj->id;
$log->version = $this->modelobj->version;
$log->target = $this->modelobj->target;
Expand Down
10 changes: 2 additions & 8 deletions analytics/tests/prediction_actions_test.php
Expand Up @@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for prediction actions.
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;

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

Expand All @@ -34,7 +28,7 @@
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_prediction_actions_testcase extends advanced_testcase {
class prediction_actions_test extends \advanced_testcase {

/**
* Common startup tasks
Expand Down
30 changes: 12 additions & 18 deletions analytics/tests/prediction_test.php
Expand Up @@ -14,21 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for evaluation, training and prediction.
*
* NOTE: in order to execute this test using a separate server for the
* python ML backend you need to define these variables in your config.php file:
*
* define('TEST_MLBACKEND_PYTHON_HOST', '127.0.0.1');
* define('TEST_MLBACKEND_PYTHON_PORT', 5000);
* define('TEST_MLBACKEND_PYTHON_USERNAME', 'default');
* define('TEST_MLBACKEND_PYTHON_PASSWORD', 'sshhhh');
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;

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

Expand All @@ -48,11 +34,19 @@
/**
* Unit tests for evaluation, training and prediction.
*
* NOTE: in order to execute this test using a separate server for the
* python ML backend you need to define these variables in your config.php file:
*
* define('TEST_MLBACKEND_PYTHON_HOST', '127.0.0.1');
* define('TEST_MLBACKEND_PYTHON_PORT', 5000);
* define('TEST_MLBACKEND_PYTHON_USERNAME', 'default');
* define('TEST_MLBACKEND_PYTHON_PASSWORD', 'sshhhh');
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_analytics_prediction_testcase extends advanced_testcase {
class prediction_test extends \advanced_testcase {

/**
* Purge all the mlbackend outputs.
Expand Down Expand Up @@ -746,7 +740,7 @@ public function test_not_null_samples() {
$samples,
$ranges
);
$dataset = phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
$dataset = \phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
'\core_analytics\analysis');
$this->assertArrayHasKey('123-0', $dataset);
$this->assertArrayHasKey('123-1', $dataset);
Expand All @@ -770,7 +764,7 @@ public function test_not_null_samples() {
$samples,
$ranges
);
$dataset = phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
$dataset = \phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
'\core_analytics\analysis');
$this->assertArrayNotHasKey('123-0', $dataset);
$this->assertArrayNotHasKey('123-1', $dataset);
Expand Down
13 changes: 4 additions & 9 deletions analytics/tests/stats_test.php
Expand Up @@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Provides the {@link analytics_stats_testcase} class.
*
* @package core_analytics
* @category test
* @copyright 2019 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;

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

Expand All @@ -31,10 +24,12 @@
/**
* Unit tests for the analytics stats.
*
* @package core_analytics
* @category test
* @copyright 2019 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_stats_testcase extends advanced_testcase {
class stats_test extends \advanced_testcase {

/**
* Set up the test environment.
Expand Down

0 comments on commit 61cedbd

Please sign in to comment.