Commit Graph

2010 Commits

Author SHA1 Message Date
jrfnl a7c19c069b GH Actions: test run - remove "dependency version" matrix key
Running against stable/lowest dependencies is relevant when a package has runtime (non-dev) dependencies.
However, PHPMailer does not have runtime dependencies.

In other words, the `dependency-version` matrix key is redundant and unused, so we may as well remove it.
2021-07-02 17:37:19 +02:00
jrfnl bc5fe4dc1f GH Actions: CS run - remove matrix
The CS run only needs to run against one PHP version, so there is no need to set up a matrix for this.
2021-07-02 17:34:58 +02:00
jrfnl decf70fa2b FilenameToTypeTest: add `@covers` tag 2021-07-02 17:25:47 +02:00
jrfnl 76aa6a06cb FilenameToTypeTest: add some additional test cases
... which should be supported based on the function docs.

Note: the "empty string" test may need looking at.... is this a bug ?
2021-07-02 17:25:45 +02:00
jrfnl 6ba68af48c FilenameToTypeTest: reorganize to use data providers
* Maintains the same test code and test cases.
* Removes code duplication.
* Makes it easier to add additional test cases in the future.
2021-07-02 17:25:44 +02:00
jrfnl 77c1d87123 Tests/reorganize: move file name to type tests to own file
As this test does not actually need an instantiated PHPMailer object, this class extends the `Yoast\PHPUnitPolyfills\TestCases\TestCase` instead of the `PHPMailer\Test\TestCase`.

Note: this doesn't move the complete test from the original test file, just select parts of the test method.
2021-07-02 17:25:42 +02:00
Marcus Bointon 35ab349e01
Merge pull request #2400 from jrfnl/feature/tests-reorganize-16
Tests: move line break normalization tests to own file
2021-07-02 16:57:52 +02:00
jrfnl 8bfb7d1e2f NormalizeBreaksTest: add `@covers` tag 2021-07-02 15:23:43 +02:00
jrfnl d6f3fea0ea NormalizeBreaksTest: add extra test
... to verify the behaviour of the `PHPMailer::normalizeBreaks()` method when the `PHPMailer::$LE` property has been customized.
2021-07-02 15:23:43 +02:00
jrfnl 14d7458826 NormalizeBreaksTest: add additional test case
... to ensure text without line breaks is returned unchanged.
2021-07-02 15:23:43 +02:00
jrfnl ade3b63428 NormalizeBreaksTest: reorganize to use data providers
* Maintains (largely) the same test code and test cases.
* Removes code duplication.
* Makes it easier to add additional test cases in the future.
2021-07-02 15:23:43 +02:00
jrfnl 0f916dd90a Tests/reorganize: move line break normalization tests to own file
As this test does not actually need an instantiated PHPMailer object, this class extends the `Yoast\PHPUnitPolyfills\TestCases\TestCase` instead of the `PHPMailer\Test\TestCase`.

Note: this doesn't move the complete tests from the original test file, just select parts of two test methods.
2021-07-02 15:23:43 +02:00
Marcus Bointon 49a2ea60ee
Merge pull request #2399 from jrfnl/feature/testcase-improve-on-static-prop-stability
TestCase: iterate on resetting of static properties
2021-07-02 12:12:46 +02:00
jrfnl f6715fd047 TestCase: iterate on resetting of static properties
The `PHPMailer::preSend()` method may alter the value of the _`protect static`_ `PHPMailer::$LE` property.

a9978d2079/src/PHPMailer.php (L1494-L1504)

This means that tests which rely on the value of the property being the default, may start to fail unexpectedly depending on the _order_ in which tests are being run.
I.e. if the test relying on the default value is run first, all will be fine, however, if that same test is run _after_ a test which calls `preSend()` on a non-Windows box with PHP < 8.0 and with `$this->Mailer` set to `mail`, the test may fail unexpectedly.

This commit fixes that by expanding the previously introduced "resetting of static properties" logic.

This commit:
* Expands the list of static properties in the `TestCase` class to include the `PHPMailer::$LE` property.
* Introduces two new methods to the `TestCase` class:
    - `resetStaticProperties()` to reset all static properties listed in the `TestCase::$PHPMailerStaticProps` property to their default values.
    - `updateStaticProperty()` to change the value of one individual static property in a class.
* The `updateStaticProperty()` method was made `public static` to allow for tests which do not need an instantiated PHPMailer object and thus extend the `Yoast\PHPUnitPolyfills\TestCases\TestCase` directly to be able to use the method as well.
    If any such tests would use the method, that particular test will be responsible for resetting the value back to the default after the test.
* The resetting of the static properties has also been moved from the `set_up()` to the `tear_down()` method.
    As mentioned above, these static properties may be used by both tests extending this `TestCase` as well as test extending the `Yoast\PHPUnitPolyfills\TestCases\TestCase`. With that in mind, resetting _after_ each test will be more stable, as long as tests which extend the `Yoast\PHPUnitPolyfills\TestCases\TestCase` and would change static properties clean up after themselves.

**Note**: I'm not including the `PHPMailer::$IcalMethods` property in this change as AFAICS, this property is not being changed anywhere in the package code and as the property is `protected`, it cannot easily be changed from within a test either (and if it would be changed by a test, that test would be responsible for resetting it).

Also note: due to limitations in the `Reflection` extension, a hard-coded list of the default values of the static properties will need to be maintained in the `TestCase`. This cannot be helped as the `Reflection` extension does not have a way to accurately retrieve the default value of static properties in a PHP cross-version compatible manner.
2021-07-02 00:17:36 +02:00
Marcus Bointon a9978d2079
Merge pull request #2398 from jrfnl/feature/testcase-minor-tweaks
TestCase: various tweaks
2021-07-01 22:50:40 +02:00
jrfnl afd0da8a20 TestCase: minor tidying up
... of the inline comments in the `TestCase` file.
2021-07-01 21:52:19 +02:00
jrfnl b97983fcf6 TestCase: add support for initializing PHPMailer with exceptions
The `PHPMailer::__construct()` method has an optional `$exceptions` property to throw exceptions on errors.

Up to now, the `PHPMailer` instance created by the `set_up()` would not pass any parameters, effectively instantiating `PHPMailer` without turning on exceptions.

In a test situation it may be useful for tests to test the behaviour of a method _with_ and _without_ the exceptions option and to test that certain exceptions are thrown and throw the correct message.

With that in mind, I'm introducing a `USE_EXCEPTIONS` class constant to the `TestCase` which can be overloaded in individual test classes and will be used by the `set_up()` method to determine whether it will be instantiated with exceptions or not.

Includes introducing an overload of the class constant in one of the test class for which it would seem appropriate at this time.
Note: this does mean that the `testDKIMSigningMail()` test will show as errored instead of failed if no SMTP connection could be made, but that IMO is the more correct status anyhow.
2021-07-01 21:50:57 +02:00
jrfnl af68fb202a TestCase::setAddress(): add support for `ReplyTo`
This allows for using the `setAddress()` method in a more consistent manner (where appropriate).

Includes introducing the use of the `setAddress()` function in a few select places.

Note: I do wonder whether this method should ever be used outside of `set_up()` and `tear_down()`, but that is for further discussion and outside the scope of this commit.
2021-07-01 19:15:07 +02:00
jrfnl 1c32844af2 TestCase: remove unused property 2021-07-01 19:03:17 +02:00
Marcus Bointon c5c35e4378
Merge pull request #2397 from jrfnl/feature/tests-ensure-static-property-starts-with-default-value
Tests: stabilize handling of static properties in PHPMailer class
2021-07-01 18:49:15 +02:00
Marcus Bointon a8967c31a2
Merge pull request #2395 from jrfnl/feature/tests-reorganize-15
Tests: move wrapText test to own file
2021-07-01 18:48:13 +02:00
jrfnl 21fd99b057 ValidateAddressTest: stabilize handling of static `$validator` property in PHPMailer class
While this class does not (currently) _change_ the `PHPMailer::$validator` property, it does rely on the default value being the expected default.

By resetting the value of the property before and after the class, this is safeguarded for the current tests.
2021-07-01 18:29:49 +02:00
jrfnl 2077a3dc31 TestCase: stabilize handling of static properties in PHPMailer class
Public static properties changed by individual tests were not being reset to their default value prior to the next test being run.
This could influence the test results of subsequent tests as static properties are not automatically reset when a new instance of a class is instantiated. See: https://3v4l.org/8s1RB

Luckily this didn't aversely affect the tests so far, but should be safeguarded for the future.

There are only three static properties in the `PHPMailer` class, with only one of these being `public`.

This commit introduces a code snippet which will reset any static properties as listed in the `$PHPMailerStaticProps` property of the `TestCase` to the default value, as also set in the `$PHPMailerStaticProps` property, at the start of the `set_up()` which is run before each test using the `TestCase`.

For now, this is only relevant for the `ValidateAddressCustomValidatorTest` test and only when the `testSetDefaultValidatorToCustom()` test would fail before resetting the property. All the same, having the reset in place by default will ensure that future tests which change this property won't introduce side-effects to other tests.
2021-07-01 18:29:30 +02:00
jrfnl b5826af2df WrapTextTest: add `@covers` tag 2021-06-30 22:45:01 +02:00
jrfnl e1bf4a0dd5 WrapTextTest: add additional test cases
... in part to document the behaviour of the function, in part to actually test it.

By the looks of it more tests are still needed though to raise the code coverage of this function to 100%.

I'd also recommend adding tests with different charset settings as the current tests are all based on the default charset (`CHARSET_ISO88591`).

Also note: the "empty string" test may need looking at.... is this a bug ?
2021-06-30 22:44:34 +02:00
jrfnl 3ed9f04ff2 WrapTextTest: reorganize to use data providers
* Maintains the existing test cases.
* Prevent one failing assertion hiding a potential second failure.
* Makes it easier to add additional test cases in the future.
2021-06-30 22:36:11 +02:00
jrfnl f9d63167df Tests/reorganize: move wrapText test to own file 2021-06-30 22:36:09 +02:00
Marcus Bointon 03edf2348c
Merge pull request #2394 from jrfnl/feature/tests-skip-pop-tests-on-windows
PopBeforeSmtpTest: skip on Windows
2021-06-30 15:31:12 +02:00
jrfnl 39426ae930 PopBeforeSmtpTest: skip on Windows
The shell commands used in the test are not available on Windows, so these tests would always fail, so we may as well skip them.
2021-06-29 12:36:07 +02:00
Juliette c717120668
Tests/reorganize: move Auth CRAM MD5 test to own file (#2392)
As this test is marked _incomplete_, no further review of the test has been done and no `@covers` tag has been added.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-29 11:22:51 +02:00
Marcus Bointon 84325b80b3
Improve simple messageid regex for default pattern 2021-06-28 10:54:41 +02:00
Marcus Bointon cb25751853
Minor tweak to MessageID regex, fixes #2388 2021-06-28 10:28:28 +02:00
Juliette 3a30a0b963
PHPMailer::createHeader: minor tweak to messageID validation (#2391)
Minor improvement to the *massive* improvement from 853afcb858.

The `$` in a regex means "match the end of a string or new line right before the end". Using the `D` modifier prevents the matching on the new line.

Ref:
* https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
* http://web.archive.org/web/20131226183006/http://blog.php-security.org/archives/76-Holes-in-most-preg_match-filters.html

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-28 10:05:36 +02:00
Marcus Bointon 853afcb858
Stricter checking of custom Message IDs, see #2388 2021-06-28 08:51:09 +02:00
Juliette a55b8b68af
Tests: move utf8CharBoundary test to own file (#2389)
* Tests/reorganize: move utf8CharBoundary test to own file

* Utf8CharBoundaryTest: reorganize to use data providers

This:
* Renames the test method and moves the description to the docblock.
* Decouples the test from the PHPMailer `TestCase` as it doesn't need the complete `set_up()` and `tear_down()`.
* Moves the test cases to a data provider.
* Adds a `@covers` tag.

* Utf8CharBoundaryTest: add `@todo` reminder

This really could do with some more test cases to properly cover all paths and branching in the method.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-28 08:51:00 +02:00
Juliette 6477e1a6d7
Tests: move address splitting tests to own file (#2387)
* Tests/reorganize: move address splitting tests to own file

As this test does not actually need an instantiated PHPMailer object, this class extends the `Yoast\PHPUnitPolyfills\TestCases\TestCase` instead of the `PHPMailer\Test\TestCase`.

* ParseAddressesTest: reorganize the testAddressSplitting() test

This commit:
* Adds two new test methods, one for testing address splitting using the PHPMailer native implementation, one for testing address splitting using the IMAP implementation.
    Both these methods use the same data provider.
* Adds a PHPUnit `@requires` tag for the IMAP extension to the IMAP version of the test.
* Adds a type check `assertIsArray()` before doing the content checks on the method return value.
* Changes the tests to do a more detailed `assertSame()` test instead of the previous `assertCount()` and `assert[Not]Empty()` checks.
* Sets up the data provider to have the detailed output arrays for comparison.

The actual test cases in the rewritten test are the same as were previously being tested.

* ParseAddressesTest: replace remaining test by dataprovider entry

The `testImapParsedAddressList_parseAddress_returnsAddressArray()` and the `testImapParsedAddressList_parseAddress_returnsAddressArray_usingImap()` tests were largely duplicates of each other, with the only real difference being the value of the second parameter passed to the `PHPMailer::parseAddresses()` method (`$useimap`).

This commit:
* Removes these two tests in favour of adding the test case to the data provider added in the previous commit.

In effect, the same test case is still being tested just as thoroughly now, just with less (test) code.

* ParseAddressesTest: add two more (invalid) test cases to `dataAddressSplitting()`

* PHPMailer::parseAddresses(): bug fix

If invalid email addresses are passed to any of the IMAP functions - in this case to `imap_rfc822_parse_adrlist()` - IMAP may generate error notices or warnings.
If nothing is done with these, they will be displayed at PHP shutdown.

The notice will look something like this, for instance:
```
Notice: Unknown: Missing or invalid host name after @ (errflg=3) in Unknown on line 0
```

This bug was exposed by the newly added unit tests. You should be able to see the issue if you run the tests over the previous commit.

As PHPMailer is not interested in invalid addresses, we can just ignore these notices and errors, but we *do* have to _clear_ them to prevent the notices from being thrown.

Refs:
* https://www.php.net/manual/en/function.imap-rfc822-parse-adrlist.php
* https://www.php.net/manual/en/function.imap-errors.php
* https://github.com/ddeboer/imap/issues/308
* https://stackoverflow.com/questions/3378469/how-to-get-rid-of-error-messages-with-phps-imap-fetchstructure

* ParseAddressesTest: reorder the test cases in `dataAddressSplitting()`

... to a slightly more sane order for humans to follow and see at a glance what is being tested..

* ParseAddressesTest: add `@covers` tags

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-25 15:39:21 +02:00
Juliette 033e2d7586
Tests: move message ID test to own file (#2386)
* Tests/reorganize: move message ID test to own file

* GetLastMessageIDTest: use the correct parameter order

For PHPUnit assertions which expect an `$expected` and a `$result` parameter, the parameter order is always `( $expected, $result, ...).

While it may not seem important to use the correct parameter order for assertions doing a straight comparison, in actual fact, it is.
The PHPUnit output when the assertions fail expects this order and the failure message will be reversed if the parameters are passed in reversed order which leads to confusion and makes it more difficult to debug a failing test.

* GetLastMessageIDTest: split into separate tests

* GetLastMessageIDTest: reorganize to use data providers

While initially still only addressing the original test cases, using a data provider here will allow for adding additional test cases more easily.

* GetLastMessageIDTest: add additional tests

Adding additional tests based on the code this is supposed to be testing.

**Note**: I've put the test cases in the "valid"/"invalid" data providers based on the current reality.
Some of the test cases I've added to the "valid" data provider _might_ actually be **_invalid_**. If that's the case, the regex used on line 2554 needs adjusting to account for them (after which the test cases can be moved to the "invalid" data provider).

* GetLastMessageIDTest: add `@covers` tags

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-25 14:26:57 +02:00
Juliette 4ad936e3b5
Tests: move line length detection tests to own file (#2385)
* Tests/reorganize: move line length detected tests to own file

* HasLineLongerThanMaxTest: various test tweaks

Minor test tweaks:
* Add `@covers` tag.
* Inline comment punctuation.
* Minor code readability tweaks.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-25 11:28:10 +02:00
Juliette 6d613a4c8b
Tests: move ICal tests to own file (#2384)
* Tests/reorganize: move ICal tests to own file

* ICalTest: reorganize to use data providers

These three tests were 99% duplicate code. Using a data provider removes the duplication, while the actual tests being run are still 100% the same, including same test count and assertion count.

* ICalTest: add additional tests

... to ensure all valid methods are checked.

Adding these extra tests is a breeze now with the data provider setup.

* ICalTest: various test tweaks

Minor test tweaks:
* Add `@covers` tag. **_<- is this correct ?_**
* Minor comment punctuation.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-24 21:18:12 +02:00
Juliette a4e3c130ae
Tests: move OAuth test to own file (#2383)
* Tests/reorganize: move OAuth test to own file

This test uses the `Yoast\PHPUnitPolyfills\TestCases\TestCase` instead of the `PHPMailer\Test\TestCase` base class as it doesn't need the `set_up()` or `tear_down()` methods from the PHPMailer base test class.

* OAuthTest: various test tweaks

Minor test tweaks:
* Add `@covers` tags.
* Add "failure" messages to assertions.
* Minor comment punctuation.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-24 19:19:36 +02:00
Juliette 8317ed0bb5
Tests: move POP before SMTP tests to separate file (#2382)
* Tests/reorganize: move POP before SMTP tests to own file

This also removed the `$pids` property and handling from the PHPMailer base `TestCase` and moves this to the POP test class.

As this test now does not actually need an instantiated PHPMailer object, this class extends the `Yoast\PHPUnitPolyfills\TestCases\TestCase` instead of the `PHPMailer\Test\TestCase`.

* PopBeforeSmtpTest: move `@group` tags

... to the class level and remove them from the individual test functions.

* PopBeforeSmtpTest: various test tweaks

Minor test tweaks:
* Add `@covers` tag at class level.
* Inline comment punctuation.
* Minor code readability tweaks.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-24 15:22:24 +02:00
Juliette d2780d4125
Tests: move tests related to DKIM to separate file (#2381)
* Tests/reorganize: move DKIM related tests to own file

* DKIMTest: stabilize removal of private key file

When an assertion in a test fails, the rest of the code within the test method is no longer executed.
This means that for a test which - like three out of the five DKIM test - creates a file, the test "clean up" via `unlink()` is also no longer executed.
It also means that one test may - unintentionally - interfere with the results of another test due to the file still existing, while it is expected to have been removed.

Aside from that, the `$privatekeyfile` variable used in the various test is the same everywhere, but not consistently used in the tests, as the `'dkim_private.pem'` is hard-coded in multiple places, which decreases maintainability.

This commit fixes both these issues by:
* Declaring a class constant with the target private key file name for use in the test method.
* Implements use of this constant throughout the tests.
* Removes the `unlink()` call from the individual tests, in favour of executing it via the test `tear_down()`, which should still be executed, even when a test has failed.
    Note: as the file also contains two tests which do not create the private key file, but for which the `tear_down()` would also be executed, the `unlink()` call is wrapped in a `file_exists()`.

* DKIMTest: move `@group` tags

... to the class level and remove them from the individual test functions.

* DKIMTest: add missing `@requires` tags

Three out of the five tests actually require the `openssl` extension. Only one was so marked.

* DKIMTest: various test tweaks

Minor test tweaks:
* Rename a test to a more specific name to allow for easier test filtering via PHPUnit.
* Add `@covers` tags (Needs review!)
* The `@see` tag is indented for code elements. For external links, the `@link` tag should be used.
* Inline comment punctuation.
* Minor code readability tweaks.

* DKIMTest: add two additional tests

... to cover the "open SSL" not available case.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-24 13:20:27 +02:00
Juliette b7910e3978
Tests: move tests for the various mail transports to separate file (#2380)
* Tests/reorganize: move email transport tests to own file

* MailTransportTest: various test tweaks

Minor test tweaks:
* Add `@covers` tags (Needs review! - especially the `testMailSend()` method seems to do more than it should)
* Check if test skipping is necessary at the start of a test method.
* Add "failure message" for each assertion in tests with multiple assertions.
* Tidy up inline comments.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-06-24 10:30:20 +02:00
Marcus Bointon 3991967c80
Only need these validator functions to be loaded for this test 2021-06-23 23:50:26 +02:00
Marcus Bointon ff1abf608c
Don't need this noise; a failing test is enough 2021-06-23 23:42:09 +02:00
Marcus Bointon 723455d1b7
Comment 2021-06-23 23:42:09 +02:00
Marcus Bointon c773286b98
Readme 2021-06-23 23:42:09 +02:00
Marcus Bointon 8de8425712
Security note 2021-06-23 23:42:09 +02:00
Marcus Bointon 482e18ee05
Gmail notes 2021-06-23 23:42:08 +02:00
Marcus Bointon e0975c2c86
Prefer SMTPS over SMTP+STARTTLS in examples 2021-06-23 23:42:08 +02:00