MDL-78167 lib: Added missing class properties in tests

In PHP 8.2 and later, setting a value to an undeclared class property is
deprecated and emits a deprecation notice.
So we need to add missing class properties that still need to be declared.

Adding @runTestsInSeparateProcesses and removing setUp() and tearDown() at the
component_test.php, so the test will run in a separate process making sure that
whatever changes happen to the in-memory version of the component class in its thread,
therefore does not impact other tests and makes it more safe.
This commit is contained in:
Meirza 2023-05-09 11:09:40 +07:00
parent 4ed782dd03
commit b7008d33ea
7 changed files with 39 additions and 32 deletions

View file

@ -77,6 +77,12 @@ abstract class base {
/** @var int Move a plugin down in the plugin order */ /** @var int Move a plugin down in the plugin order */
public const MOVE_DOWN = 1; public const MOVE_DOWN = 1;
/** @var array hold $plugin->supported in version.php */
public $supported;
/** @var int hold $plugin->incompatible in version.php */
public $incompatible;
/** /**
* Whether this plugintype supports its plugins being disabled. * Whether this plugintype supports its plugins being disabled.
* *

View file

@ -142,7 +142,7 @@ EOF;
} }
if ($results->exception !== null) { if ($results->exception !== null) {
throw new ExpectationException($results->exception, $this->session); throw new ExpectationException($results->exception, $this->getSession());
} }
$violations = $results->violations; $violations = $results->violations;

View file

@ -23,6 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* *
* @covers \core_component * @covers \core_component
* @runTestsInSeparateProcesses
*/ */
class component_test extends advanced_testcase { class component_test extends advanced_testcase {
@ -33,25 +34,6 @@ class component_test extends advanced_testcase {
*/ */
const SUBSYSTEMCOUNT = 77; const SUBSYSTEMCOUNT = 77;
public function setUp(): void {
$psr0namespaces = new ReflectionProperty('core_component', 'psr0namespaces');
$psr0namespaces->setAccessible(true);
$this->oldpsr0namespaces = $psr0namespaces->getValue(null);
$psr4namespaces = new ReflectionProperty('core_component', 'psr4namespaces');
$psr4namespaces->setAccessible(true);
$this->oldpsr4namespaces = $psr4namespaces->getValue(null);
}
public function tearDown(): void {
$psr0namespaces = new ReflectionProperty('core_component', 'psr0namespaces');
$psr0namespaces->setAccessible(true);
$psr0namespaces->setValue(null, $this->oldpsr0namespaces);
$psr4namespaces = new ReflectionProperty('core_component', 'psr4namespaces');
$psr4namespaces->setAccessible(true);
$psr4namespaces->setValue(null, $this->oldpsr4namespaces);
}
public function test_get_core_subsystems() { public function test_get_core_subsystems() {
global $CFG; global $CFG;

View file

@ -43,7 +43,7 @@ class test_output_factory extends renderer_factory_base {
* *
*/ */
public function __construct() { public function __construct() {
$this->prefixes = array('theme_child', 'theme_parent'); // Leave the construct empty to override the parent.
} }
/** /**
@ -93,6 +93,7 @@ class test_output_factory extends renderer_factory_base {
* @return string[] of classnames * @return string[] of classnames
*/ */
public function get_theme_overridden_renderer_factory_search_paths($component, $subtype = null, $target = null) { public function get_theme_overridden_renderer_factory_search_paths($component, $subtype = null, $target = null) {
$themeprefixes = ['theme_child', 'theme_parent'];
$searchtargets = array(); $searchtargets = array();
$classnames = $this->standard_renderer_classnames($component, $subtype); $classnames = $this->standard_renderer_classnames($component, $subtype);
@ -102,7 +103,7 @@ class test_output_factory extends renderer_factory_base {
// when loading the theme configs. // when loading the theme configs.
// First try the renderers with correct suffix. // First try the renderers with correct suffix.
foreach ($this->prefixes as $prefix) { foreach ($themeprefixes as $prefix) {
foreach ($classnames as $classnamedetails) { foreach ($classnames as $classnamedetails) {
if ($classnamedetails['validwithprefix']) { if ($classnamedetails['validwithprefix']) {
if ($classnamedetails['autoloaded']) { if ($classnamedetails['autoloaded']) {
@ -122,7 +123,7 @@ class test_output_factory extends renderer_factory_base {
} }
// Then try general renderer. // Then try general renderer.
foreach ($this->prefixes as $prefix) { foreach ($themeprefixes as $prefix) {
foreach ($classnames as $classnamedetails) { foreach ($classnames as $classnamedetails) {
if ($classnamedetails['validwithprefix']) { if ($classnamedetails['validwithprefix']) {
if ($classnamedetails['autoloaded']) { if ($classnamedetails['autoloaded']) {

View file

@ -44,14 +44,12 @@ class myprofilelib_test extends \advanced_testcase {
* @var \core_user\output\myprofile\tree The navigation tree. * @var \core_user\output\myprofile\tree The navigation tree.
*/ */
private $tree; private $tree;
public function setUp(): void { public function setUp(): void {
// Set the $PAGE->url value so core_myprofile_navigation() doesn't complain. // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain.
global $PAGE; global $PAGE;
$PAGE->set_url('/test'); $PAGE->set_url('/test');
$this->user = $this->getDataGenerator()->create_user(); $this->user = $this->getDataGenerator()->create_user();
$this->user2 = $this->getDataGenerator()->create_user();
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->tree = new \core_user\output\myprofile\tree(); $this->tree = new \core_user\output\myprofile\tree();
$this->resetAfterTest(); $this->resetAfterTest();
@ -87,7 +85,8 @@ class myprofilelib_test extends \advanced_testcase {
* profile of another another user. * profile of another another user.
*/ */
public function test_core_myprofile_navigation_course_without_permission() { public function test_core_myprofile_navigation_course_without_permission() {
$this->setUser($this->user2); // User without permission.
$this->setUser($this->getDataGenerator()->create_user());
$iscurrentuser = false; $iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
@ -146,7 +145,7 @@ class myprofilelib_test extends \advanced_testcase {
*/ */
public function test_core_myprofile_navigation_preference_without_permission() { public function test_core_myprofile_navigation_preference_without_permission() {
// Login as link for a user who doesn't have the capability to login as. // Login as link for a user who doesn't have the capability to login as.
$this->setUser($this->user2); $this->setUser($this->getDataGenerator()->create_user());
$iscurrentuser = false; $iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
@ -211,7 +210,7 @@ class myprofilelib_test extends \advanced_testcase {
$identityfields = explode(',', $CFG->showuseridentity); $identityfields = explode(',', $CFG->showuseridentity);
// User without permission. // User without permission.
$this->setUser($this->user2); $this->setUser($this->getDataGenerator()->create_user());
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
$reflector = new \ReflectionObject($this->tree); $reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes'); $nodes = $reflector->getProperty('nodes');
@ -300,7 +299,7 @@ class myprofilelib_test extends \advanced_testcase {
public function test_core_myprofile_navigationn_login_activity_without_permission() { public function test_core_myprofile_navigationn_login_activity_without_permission() {
// User without permission. // User without permission.
set_config("hiddenuserfields", "firstaccess,lastaccess,lastip"); set_config("hiddenuserfields", "firstaccess,lastaccess,lastip");
$this->setUser($this->user2); $this->setUser($this->getDataGenerator()->create_user());
$iscurrentuser = false; $iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);

View file

@ -711,7 +711,6 @@ class exposed_global_navigation extends global_navigation {
$page = $PAGE; $page = $PAGE;
} }
parent::__construct($page); parent::__construct($page);
$this->cache = new navigation_cache('unittest_nav');
} }
public function __call($method, $arguments) { public function __call($method, $arguments) {
if (strpos($method, $this->exposedkey) !== false) { if (strpos($method, $this->exposedkey) !== false) {
@ -763,9 +762,9 @@ class mock_initialise_global_navigation extends global_navigation {
*/ */
class exposed_navbar extends navbar { class exposed_navbar extends navbar {
protected $exposedkey = 'exposed_'; protected $exposedkey = 'exposed_';
public function __construct(\moodle_page $page) { public function __construct(\moodle_page $page) {
parent::__construct($page); parent::__construct($page);
$this->cache = new navigation_cache('unittest_nav');
} }
public function __call($method, $arguments) { public function __call($method, $arguments) {
if (strpos($method, $this->exposedkey) !== false) { if (strpos($method, $this->exposedkey) !== false) {
@ -793,7 +792,6 @@ class exposed_settings_navigation extends settings_navigation {
public function __construct() { public function __construct() {
global $PAGE; global $PAGE;
parent::__construct($PAGE); parent::__construct($PAGE);
$this->cache = new navigation_cache('unittest_nav');
} }
public function __call($method, $arguments) { public function __call($method, $arguments) {
if (strpos($method, $this->exposedkey) !== false) { if (strpos($method, $this->exposedkey) !== false) {

View file

@ -700,6 +700,27 @@ class core_testable_persistent extends persistent {
const TABLE = 'phpunit_persistent'; const TABLE = 'phpunit_persistent';
/** @var bool before validate status. */
public ?bool $beforevalidate;
/** @var bool before create status. */
public ?bool $beforecreate;
/** @var bool before update status. */
public ?bool $beforeupdate;
/** @var bool before delete status. */
public ?bool $beforedelete;
/** @var bool after create status. */
public ?bool $aftercreate;
/** @var bool after update status. */
public ?bool $afterupdate;
/** @var bool after delete status. */
public ?bool $afterdelete;
protected static function define_properties() { protected static function define_properties() {
return array( return array(
'shortname' => array( 'shortname' => array(