Skip to content

Commit

Permalink
MDL-73785 phpunit: Move more tests to use correct names and namespaces
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:

- When belonging to other components and being valid api:
  - analytics related tests have been moved to tests/analytics subdir.
  - backup & restore related tests have been moved to tests/backup subdir.
  - events related tests have been moved to tests/event subdir.
  - privacy related tests have been moved to tests/privacy.
  - task related tests have been moved to tests/task subdir.
- Some simple renames, not including the component part anymore (not
  needed now that they are namespaced):
  - some xxxlib_test.php have been renamed lib_test.php
    (when they where testing the corresponding lib.php).
  - cache stores tests have been all renamed store_test, originally
    each one had its own name (file_test, apcu_test, redis_test...)
  - assign feedback tests have been all renamed feedback_test, originally
    each one had its own name (file_test, editpdf_test...)
  • Loading branch information
stronk7 committed Mar 3, 2022
1 parent 1d99ba1 commit 4003e31
Show file tree
Hide file tree
Showing 87 changed files with 1,297 additions and 1,706 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@
// 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 scheduled tasks.
*
* @package tool_dataprivacy
* @copyright 2018 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
require_once('data_privacy_testcase.php');
namespace tool_dataprivacy\task;

use tool_dataprivacy\api;

defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../data_privacy_testcase.php');

/**
* Tests for scheduled tasks.
*
* @package tool_dataprivacy
* @copyright 2018 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_dataprivacy_task_testcase extends data_privacy_testcase {
class task_test extends \data_privacy_testcase {

/**
* Test tearDown.
Expand Down
47 changes: 24 additions & 23 deletions admin/tool/log/store/database/tests/store_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace logstore_database;

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

require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/store.php');

/**
* External database log store tests.
*
* @package logstore_database
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/store.php');

class logstore_database_store_testcase extends advanced_testcase {
class store_test extends \advanced_testcase {
/**
* Tests log writing.
*
Expand Down Expand Up @@ -120,7 +121,7 @@ public function test_log_writing(bool $jsonformat) {

$this->setUser(0);
$event1 = \logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
$event1->trigger();

$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
Expand All @@ -141,11 +142,11 @@ public function test_log_writing(bool $jsonformat) {
$this->assertEquals($data, $log1);

$this->setAdminUser();
\core\session\manager::loginas($user1->id, context_system::instance());
\core\session\manager::loginas($user1->id, \context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));

$event2 = \logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
array('context' => \context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
$event2->trigger();

\core\session\manager::init_empty_session();
Expand Down Expand Up @@ -190,30 +191,30 @@ public function test_log_writing(bool $jsonformat) {
$DB->delete_records('logstore_standard_log');

\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$store->flush();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(8, $DB->count_records('logstore_standard_log'));

// Test guest logging setting.
Expand All @@ -225,22 +226,22 @@ public function test_log_writing(bool $jsonformat) {

$this->setUser(null);
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));

$this->setGuestUser();
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));

$this->setUser($user1);
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(1, $DB->count_records('logstore_standard_log'));

$this->setUser($user2);
\logstore_database\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));

set_config('enabled_stores', '', 'tool_log');
Expand Down Expand Up @@ -270,7 +271,7 @@ public function test_is_event_ignored() {
set_config('logguests', 0, 'logstore_database');
$this->setGuestUser();
$event = \logstore_database\event\unittest_executed::create(
array('context' => context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
array('context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
$logmanager = get_log_manager();
$store = new \logstore_database\test\store($logmanager);
$this->assertTrue($store->is_event_ignored($event));
Expand Down
19 changes: 10 additions & 9 deletions admin/tool/log/store/legacy/tests/store_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace logstore_legacy;

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

require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/store.php');

/**
* Legacy log store tests.
*
* @package logstore_legacy
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/store.php');

class logstore_legacy_store_testcase extends advanced_testcase {
class store_test extends \advanced_testcase {
public function test_log_writing() {
global $DB;
$this->resetAfterTest();
Expand Down Expand Up @@ -58,12 +59,12 @@ public function test_log_writing() {

$this->setUser(0);
$event1 = \logstore_legacy\event\unittest_executed::create(
array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
array('context' => \context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
$event1->trigger();

$this->setUser($user1);
$event2 = \logstore_legacy\event\unittest_executed::create(
array('context' => context_course::instance($course2->id), 'other' => array('sample' => 6, 'xx' => 11)));
array('context' => \context_course::instance($course2->id), 'other' => array('sample' => 6, 'xx' => 11)));
$event2->trigger();

$logs = $DB->get_records('log', array(), 'id ASC');
Expand Down
Loading

0 comments on commit 4003e31

Please sign in to comment.