Commit Graph

432 Commits

Author SHA1 Message Date
Andrew Nicols b2c991e3c9 MDL-86431 phpunit: Rename test teardown method 2025-09-17 13:58:16 +08:00
Andrew Nicols 303b8a4bc1 MDL-86452 core: Reset navigation data between tests 2025-08-28 13:13:05 +08:00
Neill Magill e7a72b4354 MDL-85563 phpunit: Clock API should always use Moodle system time
When using `clock::now()` the frozen cloxk and incrementing clock will now
always return a `DateTimeImmutable` that uses the configured Moodle system
time. In unit tests this will be Australia/Perth.

Before this change, it would sometimes be UCT.
2025-06-30 11:42:23 +01:00
Daniel Ziegenberg 15579eb07f MDL-84674 core: Remove long-deprecated methods
Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
2025-03-18 10:36:53 +01:00
David Carrillo c03f47abfd MDL-83552 reportbuilder: Add custom fields to Report builder 2025-03-10 08:21:10 +01:00
Daniel Ziegenberg c23d5edb0f MDL-83334 core: Remove deprecated constant E_STRICT
One of the error levels PHP previously emitted was E_STRICT, on code
that was not strictly correct to ensure interoperability and forward
compatibility. PHP 7.0 converted the majority of existing E_STRICT
warnings to E_NOTICE, and since PHP 8.0, all E_STRICT notices have
changed to E_NOTICE.

Because all of the E_STRICT notices are upgraded to E_NOTICE since PHP
8.0, PHP 8.4 deprecates the E_STRICT constant.

The E_STRICT constant is deprecated in PHP 8.4. Using the constant
anywhere in PHP code now emits a deprecation notice in PHP 8.4 and
later.

The E_STRICT constant will be removed in PHP 9.0.

PHP core and core extensions since PHP 8.0 and later do not emit
E_STRICT notices at all. It is safe to assume that any PHP applications
that run on PHP 8.0 and later will never encounter E_STRICT notices, and
error reporting and handling can be safely updated to ignore E_STRICT
notices.

See: https://php.watch/versions/8.4/E_STRICT-deprecated

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
2025-03-04 20:38:04 +01:00
Simey Lameze 635522dc3c MDL-84343 phpunit: handle warnings on failing tests
Signed-off-by: Simey Lameze <simey@moodle.com>
2025-02-04 19:00:29 +08:00
Sara Arjona 1cbfd1b16e Merge branch 'MDL-84355-main' of https://github.com/andrewnicols/moodle 2025-01-29 08:09:43 +01:00
Andrew Nicols ee24c55435 MDL-84355 phpunit: Correct test path for component configs 2025-01-29 11:29:03 +08:00
Jake Dallimore a644d26c5e MDL-79843 core: support mocking of deprecated plugins and subplugins
This adds support to the existing, shallow fakeplugin mocks provided by:
- advanced_testcase::add_mocked_plugintype() and
- advanced_testcase::add_mocked_plugin()
and adds new methods for deep mocking fakeplugins:
- advanced_testcase::add_full_mocked_plugintype()
- advanced_testcase::deprecate_full_mocked_plugintype()
- advanced_testcase::delete_full_mock_plugintype()
Deep mocking is required to test \core\component itself.
2025-01-28 14:42:19 +08:00
Sara Arjona 2f3cffd815 Merge branch 'MDL-83468-main' of https://github.com/andrewnicols/moodle 2025-01-20 16:47:56 +01:00
Andrew Nicols 41249b16f3 MDL-83468 phpunit: Test names are not optional 2025-01-20 16:47:56 +01:00
Andrew Nicols 9536ca738e MDL-83468 phpunit: Do not throw exception in mocked destructor
PHPUnit removed the ability to mock a destructor, but our lock system
throws an exception if a lock has not been explicitly released in its
destructor.

Normally thhis is fine because the lock is released, and if not then we
want to know about it.

However, where we are mocking the lock, we do not actually obtain the
lock, and we may expect the test to fail.

This change moves the release and notification to a separate, reusable
public method, which is called from the destructor. This allows it to be
mocked at the appropriate time.
2025-01-20 16:47:56 +01:00
Andrew Nicols cb63d29ace MDL-83468 phpunit: Add back deprecated assertTag 2025-01-20 16:47:55 +01:00
Andrew Nicols bd01a91ae0 MDL-83468 phpunit: Add back getName to testcase
In some situations we use the name of the test so we must keep
`getName()` for backwards compatibility.
2025-01-20 16:47:55 +01:00
Andrew Nicols d6aabc8b2e MDL-83468 phpunit: Add back isInIsolation to testcase
This helper has been deprecated in PHPUnit, but we still need it in
Moodle.
2025-01-20 16:47:55 +01:00
Andrew Nicols bc75ef8bbc MDL-83468 phpunit: Fix various tests after phpunit 10 2025-01-20 16:47:55 +01:00
Andrew Nicols b95a8c6ecc MDL-83468 phpunit: Fix data provider param names 2025-01-20 16:47:55 +01:00
Jun Pataleta d0a31f104a Merge branch 'MDL-83172-main' of https://github.com/meirzamoodle/moodle 2025-01-15 17:02:12 +08:00
meirzamoodle c26cc0e005 MDL-83172 dml: Remove Oracle from LMS 2025-01-13 17:11:55 +07:00
Andrew Nicols c145a65b61 MDL-84152 tests: Remove extraneous / in external file URL generation 2025-01-10 14:47:50 +08:00
Andrew Nicols b29615a6f0 MDL-83335 core: Explicitly specify escape with fgetcsv/fputcsv
PHP 8.4 requires that a value be provided for the `$escape` parameter to
both `fgetcsv()` and `fputcsv()`.
2025-01-07 13:26:48 +08:00
Sara Arjona 1420e2fb5d Merge branch 'MDL-82457-main' of https://github.com/laurentdavid/moodle 2024-12-11 08:57:09 +01:00
Andrew Nicols c2414e5a45 MDL-81520 core: Fix test finality 2024-12-11 12:30:23 +08:00
Laurent David 2be6563324 MDL-82457 core: Remove chat and survey dependencies
* Remove chat and survey plugins from the standard plugins
* Remove all related tables and settings from both plugins
using upgrade.
* Remove temporary admin notification (MDL-82297) and strings.
* PHP Unit:
   - Remove chat and survey as a sample module for unit testing
     replacing it with assignment module when possible (if not page).
   - Remove chat and survey from bulk update test
* Behat: Remove chat and survey behat tests
2024-12-10 08:59:18 +01:00
Andrew Nicols 7cb2b0ca40 MDL-83412 calendartype_gregorian: Use core_date
Update timestamp_to_date_string to use core_date.
2024-11-28 11:47:30 +08:00
Andrew Nicols a6acb015a3 MDL-81521 core: Update all possibly data providers to be static
Note: Some data providers could not be automatically be converted to
being static. These will be handled in a separate issue.
2024-11-15 12:50:55 +08:00
Andrew Nicols c745b3fb80 MDL-81308 core: Use a helper for consecutive invocation counts 2024-10-23 14:14:33 +08:00
Andrew Nicols 3e2d581628 MDL-82627 PHPUnit: Add helper to mock http_client 2024-09-11 11:25:16 +07:00
Andrew Nicols 74d1f5f9ce MDL-82627 PHPUnit: Add helper to get path to fixture 2024-09-11 11:25:16 +07:00
Andrew Nicols 0270c90dc0 MDL-66903 core: Introduce \core\component::resetto replace hacks
Traditionally we have done this using reflection and it would be better
to do this in a 'proper' fashion in case we ever need to change how it
works.
2024-07-15 13:07:42 +08:00
Andrew Nicols aa55779ba0 MDL-66903 testing: Helper to load fixture files 2024-07-15 13:07:42 +08:00
Andrew Nicols 25df1ed645 MDL-66903 core: Move core_component to \core\component 2024-07-15 13:07:42 +08:00
Andrew Nicols 406dcd2566 MDL-66903 testing: Add support for a \tests\ namespace during tests
This commit:
- introduces a \tests\ sub-namespace for use in unit tests only
- the path to this the tests/classes directory of the owning parent
- files here are excluded from unit test runs

This is agreed per policy in MDL-80855.
2024-07-15 13:07:36 +08:00
Andrew Nicols 2c8acd3462 MDL-66903 testing: Reset CFG and component after test
This change moves the reset of global test state to the finally section
rather than doing it only if the test passes.

Previously if a test which modifies the `core_component` internals
failed, it would not reset the internal state and impact subsequent
tests.
2024-07-15 10:14:49 +08:00
Eloy Lafuente (stronk7) 674497a12c MDL-81523 phpunit: Add all missing setUp/tearDown parent calls
All setUp(), tearDown(), setUpBeforeClass() and tearDownAfterClass()
must, always, call to parent, to ensure that everything is properly
set and cleaned.

While in a lot of situations this is not needed (parents may not
have anything to run), with PHPUnit >= 10 this can become more
important because we are going to move the reset code from current
placement @ runBare() to setUp()/tearDown().

Note that all the changes performed in this commit have been detected
and fixed by moodle-cs (ParentSetUpTearDownSniffTest).
2024-06-14 16:04:57 +02:00
Eloy Lafuente (stronk7) 6c35a65e1a MDL-81522 phpunit: Add a few non-void return types to tests
This is the final (3rd) commit, where we are adding missing
return types to a few tests (using @depends) which return
types are non-void, but something really returned (for consume
by the dependent test).
2024-06-11 11:55:08 +02:00
Eloy Lafuente (stronk7) 4f7631113c MDL-81522 phpunit: Add missing void return type to all tests #2
This commit includes more changes, all them also adding the :void
return type to unit tests missing them.

The difference is that all these changes, while also detected
perfectly by the moodle.PHPUnit.TestReturnType sniff, were not
auto-fixed (like the previous commit ones), because all them
do include some "return" statement and, for safety, we don't
fix them.

All the cases have been visually inspected and confirmed that
the existing "return" statements always belong to anon
functions within the test body and not the test own return statement.
2024-06-11 11:55:08 +02:00
Eloy Lafuente (stronk7) 01148a0816 MDL-81522 phpunit: Add missing void return type to all tests
While this change is not 100% required now, it's good habit
and we are checking for it since Moodle 4.4.

All the changes in this commit have been applied automatically
using the moodle.PHPUnit.TestReturnType sniff and are, exclusively
adding the ": void" return types when missing.
2024-06-11 11:55:07 +02:00
Eloy Lafuente (stronk7) 76e41b936d MDL-81698 phpunit: Convert some skipped tests into useful ones
After some tests, it seems that we can safely cover
phpunit_util::reset_all_data() executing it within
own basic_test self tests.

That way we can confirm that the reset code is doing its job
and detecting unexpected changes at various levels (database,
globals, ...).

Note that, in order to catch the E_USER_WARNINGS, for PHPUnit 9.6
and up, we have to convert them to exceptions, because the notice/
warning/error expectations have been deprecated and will be removed
in PHPUnit 10. So we are using a trick, already used also by
advanced_test. And, no matter that we are repeating the trick
a few times, that's ok in order to have all its uses controlled.
2024-05-25 17:19:44 +02:00
Eloy Lafuente (stronk7) 6fe0e66d06 MDL-81698 phpunit: Uncomment some tests and make them skipped
There are a few tests in core that are commented since the
beginning. That's not useful at all, so with this commit we
are un-commenting them instead.

Note that this is an initial step to have them back with skipped
outcome. Later in this issue we'll decide about to keep them or
completely remove them (each test can have a different outcome).
2024-05-25 17:19:43 +02:00
Eloy Lafuente (stronk7) 71bd82ad68 MDL-81581 phpunit: Create the normalise_line_endings() method
And apply it to all the obvious places related with the issue.

Note that surely there are way more in code base, but it's
out of scope for this issue.
2024-04-25 18:52:16 +02:00
Andrew Nicols 6f1df841f7 MDL-67667 core: Deprecate and removal task blocking
There are inherent issues with task blocking which mean that it has
never worked properly. It is also very buggy and can lead to massive
performance issues with task processing.

It is almost impossible to deprecate this in a staged fashion because
various APIs use the methods and it is not possible to determine which
are API uses and which are other valid uses.

In reality there has been little-to-no uptake on the use of this feature
and it should just be removed.
2024-04-12 12:34:20 +08:00
Andrew Nicols 62c560dff2 MDL-81456 core: Correct namespace imports 2024-04-09 16:13:42 +08:00
Andrew Nicols d7e1f2cef7 MDL-81456 core: Run tests modifying core_component in a separate process 2024-04-08 09:36:21 +08:00
Andrew Nicols fa2e11105a MDL-81456 core: Stop injecting test data into real hook manager 2024-04-08 09:36:21 +08:00
Andrew Nicols 2b49ad42f5 MDL-81456 core: Use DI for all hook access
Using DI for all hook access means that it becomes significantly easier
to mock hooks and callbacks for unit testing without fundamentally
altering the structure of the code purely for the purposes of unit
testing.
2024-04-08 09:35:41 +08:00
Andrew Nicols 298c13ac3b MDL-80838 core: Add PSR-20/Clock support
This commit adds the PSR-20 ClockInterface to core, with a
moodle-specific extension to the Interface at `\core\clock`, and a
standard clock at `\core\system_clock`.

Further clocks are provided as `\incrementing_clock` and `\frozen_clock`
which are available to unit tests using:

- `$this->mock_clock_with_incrementing(?int $starttime = null);`
- `$this->mock_clock_with_frozen(?int $time = null);`

For the incrementing clock, every call to fetch the time will bump the
current time by one second.

For the frozen clock the time will not change, but can be modified with:
- `$clock->set_to(int $time);`; and
- `$clock->bump(int $seconds = 1);`
2024-04-02 10:24:54 +08:00
Jun Pataleta 556347c4f1 Merge branch 'MDL-81266' of https://github.com/stronk7/moodle 2024-03-26 10:34:47 +08:00
Eloy Lafuente (stronk7) 4c2f8b3a91 MDL-81266 phpunit: Remove expect(Deprecation|Notice|Warning|Error)
PHPUnit 9.6 has deprecated all the expect(Deprecation|Notice|Warning|Error)
assertions, so we have to move away from them.

In core, we only had 2 cases, one easily fixed by getting rid of it,
because, for php >= 80 it's an assert-able exception.

And the other replaced with code that, temporarily, sets a custom
error handler that converts any specified E_ to an asset-able
exception.

Note that tests playing with error handlers should, always, be
run in separate process, to avoid problems or conflicts with
PHPUnit / Moodle own error handlers.
2024-03-25 16:21:53 +01:00