Commit Graph

256 Commits

Author SHA1 Message Date
David Carrillo c03f47abfd MDL-83552 reportbuilder: Add custom fields to Report builder 2025-03-10 08:21:10 +01: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
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
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) 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
Andrew Nicols 624a1d8169 MDL-81074 core: Add assertTimeStringMatches PHPUnit Assertion
Also raised as https://github.com/sebastianbergmann/phpunit/issues/5757
2024-03-25 09:51:25 +08:00
Ilya Tregubov 9f10899178 Merge branch 'MDL-80099_main' of https://github.com/marxjohnson/moodle 2024-03-22 11:07:55 +08:00
Eloy Lafuente (stronk7) 0f3775088f MDL-69688 phpunit: Improve test_context_not_used
This adds a few changes to the old test_context_not_used test:

- Move it to become an advanced_test, because its mission
  is to verify that the assertEventContextNotUsed() assertion
  works as expected.
- For consistency, also move the fixtures to own phpunit fixtures.
- Add proper coverage tags, to verify that the assertion is being
  covered.
- Add a data provider to provide all the current cases and ease
  any future case that may be needed in the future. One by one
  because previously there was code never executed with the
  warning expectation causing the test to stop.
- Run them in isolation, while this is not strictly required, it's
  including external fixtures and, we'll need that isolation soon
  (for changes coming when moving the test to PHPUnit 9.6 in MDL-81266).
2024-03-21 08:31:08 +01:00
Mark Johnson 788556f652 MDL-80099 hooks: Add mechanism to deprecate class callbacks 2024-03-20 14:58:16 +00:00
Eloy Lafuente (stronk7) 361dfe8145 MDL-75952 general: Since php81, refection->setAccessible() is no-op
Refereces:
- https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
- https://www.php.net/manual/en/reflectionproperty.setaccessible.php
- https://www.php.net/manual/en/reflectionmethod.setaccessible.php

As of PHP 8.1.0, calling this method has no effect; all methods are
invokable by default. So, let's remove all uses from core, they are
no-op.
2024-03-10 21:15:00 +01:00
Jun Pataleta 3278ce7aba Merge branch 'MDL-65292' of https://github.com/stronk7/moodle 2024-03-08 08:00:41 +08:00
Sara Arjona b2e5f3e746 MDL-80866 phpunit: Fix failures after disabling Chat and Survey 2024-03-01 11:41:50 +01:00
Eloy Lafuente (stronk7) ba1f804ffa MDL-65292 style: Fix all function declarations white space
This has been generated running the following Sniffs, all
them part of the Moodle's CodeSniffer standard:
- PSR12.Functions.ReturnTypeDeclaration
- PSR12.Functions.NullableTypeDeclaration
- moodle.Methods.MethodDeclarationSpacing
- Squiz.Whitespace.ScopeKeywordSpacing

All them are, exclusively, about correct spacing, so the changes
are, all them, only white space changes.

Only exceptions to the above are 3 changes what were setting the
return type in a new line, and, when that happens, the closing
parenthesis (bracket) has to go to the same line than the colon.
2024-02-28 23:33:26 +01:00
Eloy Lafuente (stronk7) 6e2c5b7b45 MDL-80844 phpunit: Only some tests can use the debugging sink
The only tests that have COMPLETE* support for the debugging sink
are the advanced_testcase and the database_driver ones (store and
report). So we must ensure that the rest of tests don't use the
debugging sink at all.

Right now we are using it for storing, but later there is not
reporting, so any debugging happening within non advanced tests
is not detected.

This commit just ensures that we stop making that storing for
non advanced/database_driver tests. Nothing more, nothing less.

* Note that we have had to add a few missing bits to the
database_driver testcase because it was not 100% complete. Now
it behaves 100% the same than the advanced_testcase one regarding
the debugging sink.
2024-02-15 18:46:16 +01:00
Andrew Nicols 9ed3f83dd2 MDL-80072 core: Add \core\di wrapper to php-di
The \core\di class is a Moodle wrapper to php-di which is intended to
allow Moodle to switch to an alternate DI solution in the future if
required. All interaction with the container uses the PSR-11 Container
interfaces, which allows for normalisation of configuration, setting,
and retrieving of DI container-identified classes.
2024-02-12 11:11:16 +08:00
Huong Nguyen 149dd7bfa9 MDL-80713 Unit Tests: Message sink improvement
- Added a new method called get_messages_by_component()
  This method will help to retreive the redirected messages
  of specific component only
- Added a new method called get_messages_by_component_and_type()
  This method will help to retreive the redirected messages
  of specific component and type only
2024-02-05 09:23:41 +07:00
Andrew Nicols 49fc711a07 MDL-79637 testing: Update for current coding standards 2023-10-11 09:39:53 +08:00
Andrew Nicols 99a6cd12b2 MDL-79637 phpunit: Do not reset DB during init of isolated tests
During the bootstrap of PHPUnit we ensure that the database has been
reset to its initial state.

We do this by checking the internally-stored DB write count between
runs. If the count is not yet set (null), or it has been increased, we
force a reset.

When running an isolated test the test runner resets the database, it
then sets up a new isolated test environment by writing a new PHPUnit
test case and passing it to a new PHP Process using standard in. As part
of this, the bootstrap is run for that process.

Because we are in a new process, the db write count is fresh and not yet
set. This has been leading to an additional db reset before the isolated
test.

To handle this we want to _not_ perform a reset during the
initialisation for isolated runs. We know that the DB is in a fresh
state before we start the run.

To support this we need to know whether the test is an isolated test
during the bootstrap, which means we cannot use the previous approach to
calculating this.

Instead we look at the PHP_SELF value. PHP sets this to "Standard input
code" when run from stdin, instead of running a file.

There should not be any other legitimate reason to run a PHPUnit
bootstrap via this stdin approach.

Unfortunately this approach is a little bit risky as it depends on the
presence of a specific string, however this string has been in place
since 2016, and there is no legitimate way of calculating this.

I did consider looking at whether the called script included `/vendor/`
and `/phpunit`, but this is also likely a risky approach if someone
calls PHPUnit in an unexpected way.

This approach is itself unit tested so any change to PHP's stdin string
before we deprecate this approach entirely in 12 months time will be
caught.
2023-10-11 09:29:26 +08:00
Petr Skoda b2a2d3dc66 MDL-79338 core: add support for hook callback redirection in tests 2023-09-14 11:47:52 +02:00
Tomo Tsuyuki d8a4cc9533 MDL-67186 group: add custom fields 2023-08-09 11:02:49 +10:00
Sara Arjona c1d805a676 Merge branch 'MDL-78738-master' of https://github.com/andrewnicols/moodle 2023-08-01 16:24:42 +02:00
Andrew Nicols 235d5b9b72 MDL-78738 core_testing: Add helper to run adhoc tasks 2023-08-01 19:50:13 +08:00