From 51533eb252123b6020dd495988ad2ee8fda1ab30 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Tue, 10 Oct 2023 11:46:35 -0400 Subject: [PATCH 1/2] Add plugin tests Signed-off-by: Joe Fusco --- .../integration/BlocksFunctionsTests.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 plugins/faustwp/tests/integration/BlocksFunctionsTests.php diff --git a/plugins/faustwp/tests/integration/BlocksFunctionsTests.php b/plugins/faustwp/tests/integration/BlocksFunctionsTests.php new file mode 100644 index 000000000..afbd3a662 --- /dev/null +++ b/plugins/faustwp/tests/integration/BlocksFunctionsTests.php @@ -0,0 +1,103 @@ +testDir = trailingslashit( $uploads['basedir'] ) . 'test_directory'; + $this->testZip = trailingslashit( $uploads['basedir'] ) . 'test.zip'; + + // Create test directory if it doesn't exist. + if ( ! is_dir( $this->testDir ) ) { + mkdir( $this->testDir, 0777, true ); + } + } + + /** + * Tear down test environment. + */ + public function tearDown(): void { + // Remove the test directory if it exists. + if ( is_dir( $this->testDir ) ) { + rrmdir( $this->testDir ); + } + + // Remove the test zip file if it exists. + if ( file_exists( $this->testZip ) ) { + unlink( $this->testZip ); + } + + parent::tearDown(); + } + + /** + * Test the unzip_to_directory function for success scenario. + */ + public function testUnzipToDirectory() { + // Create a dummy zip file for testing. + $zip = new \ZipArchive(); + $zip->open( $this->testZip, \ZipArchive::CREATE ); + $zip->addFromString( 'testfile.txt', 'Test content' ); + $zip->close(); + + // Check if the function successfully unzips and removes the zip file. + $this->assertTrue( unzip_to_directory( $this->testZip, $this->testDir ) ); + $this->assertFileExists( $this->testDir . '/testfile.txt' ); + $this->assertFalse( file_exists( $this->testZip ) ); + } + + /** + * Test the unzip_to_directory function for failure scenario. + */ + public function testUnzipToDirectoryFail() { + // Test the function with a non-existent zip file. + $this->assertFalse( unzip_to_directory( 'nonexistent.zip', $this->testDir ) ); + } + + /** + * Test the rrmdir function. + */ + public function testRrmdir() { + // Create a directory structure to test the rrmdir function. + mkdir( $this->testDir . '/subdir' ); + touch( $this->testDir . '/file.txt' ); + touch( $this->testDir . '/subdir/file2.txt' ); + + // Test if rrmdir successfully removes the directory and its contents. + rrmdir( $this->testDir ); + $this->assertFalse( is_dir( $this->testDir ) ); + } +} From 77574e2238964c89f50e28193cc2cb78839d12a8 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Tue, 10 Oct 2023 11:59:46 -0400 Subject: [PATCH 2/2] Add test coverage for utility functions Signed-off-by: Joe Fusco --- .../integration/BlocksFunctionsTests.php | 103 ----------------- .../integration/UtilitiesFunctionsTests.php | 107 ++++++++++++++++++ 2 files changed, 107 insertions(+), 103 deletions(-) delete mode 100644 plugins/faustwp/tests/integration/BlocksFunctionsTests.php create mode 100644 plugins/faustwp/tests/integration/UtilitiesFunctionsTests.php diff --git a/plugins/faustwp/tests/integration/BlocksFunctionsTests.php b/plugins/faustwp/tests/integration/BlocksFunctionsTests.php deleted file mode 100644 index afbd3a662..000000000 --- a/plugins/faustwp/tests/integration/BlocksFunctionsTests.php +++ /dev/null @@ -1,103 +0,0 @@ -testDir = trailingslashit( $uploads['basedir'] ) . 'test_directory'; - $this->testZip = trailingslashit( $uploads['basedir'] ) . 'test.zip'; - - // Create test directory if it doesn't exist. - if ( ! is_dir( $this->testDir ) ) { - mkdir( $this->testDir, 0777, true ); - } - } - - /** - * Tear down test environment. - */ - public function tearDown(): void { - // Remove the test directory if it exists. - if ( is_dir( $this->testDir ) ) { - rrmdir( $this->testDir ); - } - - // Remove the test zip file if it exists. - if ( file_exists( $this->testZip ) ) { - unlink( $this->testZip ); - } - - parent::tearDown(); - } - - /** - * Test the unzip_to_directory function for success scenario. - */ - public function testUnzipToDirectory() { - // Create a dummy zip file for testing. - $zip = new \ZipArchive(); - $zip->open( $this->testZip, \ZipArchive::CREATE ); - $zip->addFromString( 'testfile.txt', 'Test content' ); - $zip->close(); - - // Check if the function successfully unzips and removes the zip file. - $this->assertTrue( unzip_to_directory( $this->testZip, $this->testDir ) ); - $this->assertFileExists( $this->testDir . '/testfile.txt' ); - $this->assertFalse( file_exists( $this->testZip ) ); - } - - /** - * Test the unzip_to_directory function for failure scenario. - */ - public function testUnzipToDirectoryFail() { - // Test the function with a non-existent zip file. - $this->assertFalse( unzip_to_directory( 'nonexistent.zip', $this->testDir ) ); - } - - /** - * Test the rrmdir function. - */ - public function testRrmdir() { - // Create a directory structure to test the rrmdir function. - mkdir( $this->testDir . '/subdir' ); - touch( $this->testDir . '/file.txt' ); - touch( $this->testDir . '/subdir/file2.txt' ); - - // Test if rrmdir successfully removes the directory and its contents. - rrmdir( $this->testDir ); - $this->assertFalse( is_dir( $this->testDir ) ); - } -} diff --git a/plugins/faustwp/tests/integration/UtilitiesFunctionsTests.php b/plugins/faustwp/tests/integration/UtilitiesFunctionsTests.php new file mode 100644 index 000000000..68ca77bec --- /dev/null +++ b/plugins/faustwp/tests/integration/UtilitiesFunctionsTests.php @@ -0,0 +1,107 @@ +testDir = sys_get_temp_dir() . '/faustwp_test_directory'; + $this->testZip = sys_get_temp_dir() . '/test.zip'; + } + + /** + * Cleanup runs after every test. + */ + protected function tearDown(): void { + if ( is_dir( $this->testDir ) ) { + Utilities\rrmdir( $this->testDir ); + } + + if ( file_exists( $this->testZip ) ) { + unlink( $this->testZip ); + } + + parent::tearDown(); + } + + /** + * Test the camelcase function. + */ + public function testCamelcase() { + $this->assertSame( 'helloWorld', Utilities\camelcase( 'Hello world' ) ); + $this->assertSame( 'helloWorldTest', Utilities\camelcase( 'Hello-world-test' ) ); + $this->assertSame( 'hello1World2Test', Utilities\camelcase( 'Hello 1-world-2 test' ) ); + $this->assertSame( 'helloWorld$Test', Utilities\camelcase( 'Hello world $ test', array( '$' ) ) ); + $this->assertSame( 'helloWorld', Utilities\camelcase( ' Hello world ' ) ); // Test with extra spaces + } + + /** + * Test the plugin_version function. + */ + public function testPluginVersion() { + // This test assumes FAUSTWP_FILE is defined correctly. + $this->assertIsString( Utilities\plugin_version() ); + } + + /** + * Test the unzip_to_directory function. + */ + public function testUnzipToDirectory() { + // Create a dummy zip file for testing. + $zip = new \ZipArchive(); + $zip->open( $this->testZip, \ZipArchive::CREATE ); + $zip->addFromString( 'testfile.txt', 'Test content' ); + $zip->close(); + + $this->assertTrue( Utilities\unzip_to_directory( $this->testZip, $this->testDir ) ); + $this->assertFileExists( $this->testDir . '/testfile.txt' ); + $this->assertFalse( file_exists( $this->testZip ) ); + + // Test non-existent file. + $this->assertFalse( Utilities\unzip_to_directory( 'nonexistent.zip', $this->testDir ) ); + } + + /** + * Test the rrmdir function. + */ + public function testRrmdir() { + mkdir( $this->testDir . '/subdir', 0777, true ); + touch( $this->testDir . '/file.txt' ); + touch( $this->testDir . '/subdir/file2.txt' ); + + Utilities\rrmdir( $this->testDir ); + $this->assertFalse( is_dir( $this->testDir ) ); + + // Test rrmdir on non-existent directory. + Utilities\rrmdir( $this->testDir ); + $this->assertFalse( is_dir( $this->testDir ) ); + } +}