400 Commits

Author SHA1 Message Date
Andrew Nicols
d36cf65ef3
MDL-66903 core: Introduce \core_component::reset to 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 12:22:19 +08:00
Andrew Nicols
39d46917bb
MDL-66903 testing: Helper to load fixture files 2024-07-15 12:22:17 +08:00
Andrew Nicols
6b3d8fd889
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 12:21:54 +08:00
Andrew Nicols
6cd08cc382
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 12:21:06 +08:00
Eloy Lafuente (stronk7)
2a86638a15
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 12:18:05 +02:00
Eloy Lafuente (stronk7)
091b458c52
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 12:18:05 +02:00
Eloy Lafuente (stronk7)
1093256560
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 12:18:04 +02:00
Eloy Lafuente (stronk7)
86b45212e8
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:52 +02:00
Eloy Lafuente (stronk7)
80f521eed0
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:51 +02:00
Eloy Lafuente (stronk7)
c9d48f6f25
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:25 +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
Daniel Ziegenberg
87267da39d
MDL-81281 phpunit: assertObjectNotHasAttribute is deprecated
To be integrated as part of MDL-81266

When running PHPUnit 9.6 we get the following deprecation warnings:
"assertObjectNotHasAttribute() is deprecated and will be removed in PHPUnit
10. Refactor your test to use assertObjectNotHasProperty() instead."

So we replace all instances of assertObjectNotHasAttribute with
assertObjectNotHasProperty.

PHPUnit justifies the change with:
> PHPUnit currently refers to "fields" (see above) as "attributes". This
> is (or will become) confusing considering the introduction of
> attributes in PHP 8 and their support in PHPUnit.  PHPUnit will be
> changed to use the term "property" instead of "attribute" where "field"
> is meant.

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
2024-03-25 16:21:07 +01: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
Sara Arjona
cc9430929d
MDL-77708 docs: Update references from docs.moodle.org/dev 2023-09-26 10:30:19 +02: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
Paul Holden
8c7158e224
MDL-71067 phpunit: final removal of deprecated 311 coverage properties. 2023-05-26 13:40:35 +01:00
Petr Skoda
99a7a7c782 MDL-74954 core: add Hooks based on PSR-14 2023-05-20 11:03:46 +02:00
Andrew Nicols
1a53cbbae4 MDL-77989 testing: Add test file isolation helper
When we deprecate the use of a file, we often include tests which ensure
that the legacy behaviour is maintained. There are also legacy uses
in the community where people would like to use the deprecated API for a
period.

The issue that we face is that, if the deprecated file is included once,
then it will be included for all other, unrelated, tests. This means
that other tests may not detect cases where the deprecated file was
included.

We can solve these cases by running the test that performs the inclusion
in a deprecated process. This means that the inclusion is only performed
in that isolated process, and other unrelated tests do not include the
file.

However, we also then need to detect which files which are including the
file and which we do not know about.

This change introduces:
- an override to the TestCase::setInIsolation method to define a
  constant when the test is running in isolation
- a new function that a file can call when it is included, to make sure
  that the test process was isolated, where there is any test.
2023-04-21 13:33:56 +08:00
Sara Arjona
9ce4fb65fd Merge branch 'MDL-77164-master' of https://github.com/marinaglancy/moodle 2023-04-13 14:00:18 +02:00
Marina Glancy
8fc1486d36 MDL-77164 various: fix incorrect phpdocs 2023-04-13 11:35:06 +01:00
Andrew Nicols
5319ba9c15 MDL-77837 phpunit: Ensure that the cron user setter is used
When running an adhoc task in a unit test we should use the cron variant
of the set user method to mimic the behaviour of a real cron run.
2023-04-06 23:19:01 +08:00
Dmitrii Metelkin
c11d4284be MDL-77130 cohort: add custom fields 2023-04-04 13:01:40 +10:00
Ilya Tregubov
f4891b1034 Merge branch 'MDL-69882' of https://github.com/paulholden/moodle 2023-03-17 13:04:33 +01:00
Paul Holden
6b6e7eaad2
MDL-69882 phpunit: final removal of deprecated 310 helper methods. 2023-03-13 17:46:44 +00:00
Andrew Nicols
9ec46c232d MDL-77186 core: Move all uses of cronlib to new class 2023-03-13 21:21:13 +08:00
Mathew May
e5ca7766e7 MDL-52805 core: Remove legacy log calls 2023-03-07 13:08:46 +08:00
Eloy Lafuente (stronk7)
83b490a594 MDL-75111 phpunit: Move tests to use correct names and ns (take#4)
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.
2022-08-26 16:34:20 +02:00
Paul Holden
2d6139242c MDL-74671 phpunit: remove file/directory coverage duplicates. 2022-06-27 10:16:14 +01:00