From 49bcd4a0354c08c37e8993208b25e75428091212 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 9 Jul 2021 07:34:40 +0200 Subject: [PATCH] Tests: introduce dedicated tests for the `PHPMailer::fileIsAccessible()` method So far, this method did not have dedicated tests. The test file this commit introduces, tests nearly all aspects of the method as well as documents the current behaviour of the method. There is one particular test case missing. This is annotated in the class docblock. While this method is quite simple, testing it separately means that the tests for methods _using_ this method don't have to _also_ test the functioning of this method, which means they can be more focussed on their own logic. --- .../FileIsAccessibleTest/accessible.txt | 1 + .../FileIsAccessibleTest/inaccessible.txt | 1 + test/PHPMailer/FileIsAccessibleTest.php | 104 ++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 test/Fixtures/FileIsAccessibleTest/accessible.txt create mode 100644 test/Fixtures/FileIsAccessibleTest/inaccessible.txt create mode 100644 test/PHPMailer/FileIsAccessibleTest.php diff --git a/test/Fixtures/FileIsAccessibleTest/accessible.txt b/test/Fixtures/FileIsAccessibleTest/accessible.txt new file mode 100644 index 00000000..6227a9dc --- /dev/null +++ b/test/Fixtures/FileIsAccessibleTest/accessible.txt @@ -0,0 +1 @@ +Testing 1-2-3 \ No newline at end of file diff --git a/test/Fixtures/FileIsAccessibleTest/inaccessible.txt b/test/Fixtures/FileIsAccessibleTest/inaccessible.txt new file mode 100644 index 00000000..6227a9dc --- /dev/null +++ b/test/Fixtures/FileIsAccessibleTest/inaccessible.txt @@ -0,0 +1 @@ +Testing 1-2-3 \ No newline at end of file diff --git a/test/PHPMailer/FileIsAccessibleTest.php b/test/PHPMailer/FileIsAccessibleTest.php new file mode 100644 index 00000000..1a1b0929 --- /dev/null +++ b/test/PHPMailer/FileIsAccessibleTest.php @@ -0,0 +1,104 @@ + + * @author Andy Prevost + * @copyright 2012 - 2020 Marcus Bointon + * @copyright 2004 - 2009 Andy Prevost + * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License + */ + +namespace PHPMailer\Test\PHPMailer; + +use PHPMailer\PHPMailer\PHPMailer; +use ReflectionMethod; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; + +/** + * Test file accessibility verification functionality. + * + * {@internal There is one test case known to be missing, which is a test + * with a valid UNC path. If someone can figure out a way to add a test for + * that case, that would be awesome!} + * + * @covers \PHPMailer\PHPMailer\PHPMailer::fileIsAccessible + */ +final class FileIsAccessibleTest extends TestCase +{ + + /** + * Verify whether the "is a file accessible" check works correctly. + * + * @dataProvider dataFileIsAccessible + * + * @param string $input A relative or absolute path to a file. + * @param bool $expected The expected function return value. + */ + public function testFileIsAccessible($input, $expected) + { + $reflMethod = new ReflectionMethod(PHPMailer::class, 'fileIsAccessible'); + $reflMethod->setAccessible(true); + $result = $reflMethod->invoke(null, $input); + $reflMethod->setAccessible(false); + + self::assertSame($expected, $result); + } + + /** + * Data provider. + * + * @return array + */ + public function dataFileIsAccessible() + { + $fixturesPath = dirname(__DIR__) . '/Fixtures/FileIsAccessibleTest/'; + + return [ + 'Valid: accessible file' => [ + 'input' => $fixturesPath . 'accessible.txt', + 'expected' => true, + ], + 'Invalid: path not permitted' => [ + 'input' => 'https://github.com/PHPMailer/PHPMailer/', + 'expected' => false, + ], + 'Invalid: file does not exist' => [ + 'input' => $fixturesPath . 'thisfiledoesnotexist.txt', + 'expected' => false, + ], + 'Invalid: file in UNC path does not exist' => [ + 'input' => '\\\\nowhere\nothing', + 'expected' => false, + ], + ]; + } + + /** + * Test that the "is a file accessible" check correctly fails when the file permissions make + * the file unreadable. + */ + public function testFileIsAccessibleFailsOnUnreadableFile() + { + if (\DIRECTORY_SEPARATOR === '\\') { + // Windows does not respect chmod permissions. + $this->markTestSkipped('This test requires a non-Windows OS.'); + } + + $path = dirname(__DIR__) . '/Fixtures/FileIsAccessibleTest/'; + $file = $path . 'inaccessible.txt'; + chmod($file, octdec('0')); + + $reflMethod = new ReflectionMethod(PHPMailer::class, 'fileIsAccessible'); + $reflMethod->setAccessible(true); + $result = $reflMethod->invoke(null, $file); + $reflMethod->setAccessible(false); + + // Reset to the default for git files before running assertions. + chmod($file, octdec('644')); + + self::assertFalse($result); + } +}