From 591c728ac389e62e935f9998bcd91374be5c2f87 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Tue, 24 Oct 2023 16:50:43 -0700 Subject: [PATCH] Add unit tests for File class --- .../Internal/Admin/Logging/FileV2/File.php | 3 + .../Admin/Logging/FileV2/FileTest.php | 110 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 plugins/woocommerce/tests/php/src/Internal/Admin/Logging/FileV2/FileTest.php diff --git a/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php b/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php index bd28a030fbc..386197ca274 100644 --- a/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php +++ b/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php @@ -89,6 +89,9 @@ class File { } $this->source = substr( $this->source, 0, $rotation_marker ); + if ( count( $segments ) < 5 ) { + $this->hash = $this->source; + } } } diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Logging/FileV2/FileTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Logging/FileV2/FileTest.php new file mode 100644 index 00000000000..4e2a6e252fd --- /dev/null +++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Logging/FileV2/FileTest.php @@ -0,0 +1,110 @@ +assertEquals( 'test-Source_1-1-2023-10-23-' . wp_hash( 'cheddar' ) . '.log', $file->get_basename() ); + $this->assertEquals( 'test-Source_1-1', $file->get_source() ); + $this->assertNull( $file->get_rotation() ); + $this->assertEquals( strtotime( '2023-10-23' ), $file->get_created_timestamp() ); + $this->assertEquals( wp_hash( 'cheddar' ), $file->get_hash() ); + } + + /** + * @testdox Check that all properties are populated correctly when a File instance receives a rotated filename in the standard format. + */ + public function test_initialize_file_standard_rotated() { + $filename = Constants::get_constant( 'WC_LOG_DIR' ) . 'test-Source_1-1.3-2023-10-23-' . wp_hash( 'cheddar' ) . '.log'; + $resource = fopen( $filename, 'a' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors + fclose( $resource ); + $file = new File( $filename ); + + $this->assertEquals( 'test-Source_1-1.3-2023-10-23-' . wp_hash( 'cheddar' ) . '.log', $file->get_basename() ); + $this->assertEquals( 'test-Source_1-1', $file->get_source() ); + $this->assertEquals( 3, $file->get_rotation() ); + $this->assertEquals( strtotime( '2023-10-23' ), $file->get_created_timestamp() ); + $this->assertEquals( wp_hash( 'cheddar' ), $file->get_hash() ); + } + + /** + * @testdox Check that all properties are populated correctly when a File instance receives a filename in a non-standard format. + */ + public function test_initialize_file_non_standard() { + $filename = Constants::get_constant( 'WC_LOG_DIR' ) . 'test-Source_1-1-' . wp_hash( 'cheddar' ) . '.log'; + $resource = fopen( $filename, 'a' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors + fclose( $resource ); + $file = new File( $filename ); + + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ) . '.log', $file->get_basename() ); + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ), $file->get_source() ); + $this->assertNull( $file->get_rotation() ); + $this->assertEquals( filemtime( $filename ), $file->get_created_timestamp() ); + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ), $file->get_hash() ); + } + + /** + * @testdox Check that all properties are populated correctly when a File instance receives a rotated filename in a non-standard format. + */ + public function test_initialize_file_non_standard_rotated() { + $filename = Constants::get_constant( 'WC_LOG_DIR' ) . 'test-Source_1-1-' . wp_hash( 'cheddar' ) . '.5.log'; + $resource = fopen( $filename, 'a' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors + fclose( $resource ); + $file = new File( $filename ); + + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ) . '.5.log', $file->get_basename() ); + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ), $file->get_source() ); + $this->assertEquals( 5, $file->get_rotation() ); + $this->assertEquals( filemtime( $filename ), $file->get_created_timestamp() ); + $this->assertEquals( 'test-Source_1-1-' . wp_hash( 'cheddar' ), $file->get_hash() ); + } + + /** + * @testdox The delete method should delete the file from the filesystem. + */ + public function test_delete() { + $filename = Constants::get_constant( 'WC_LOG_DIR' ) . 'test-Source_1-1-' . wp_hash( 'cheddar' ) . '.5.log'; + $resource = fopen( $filename, 'a' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors + fclose( $resource ); + $file = new File( $filename ); + + $files = glob( trailingslashit( realpath( Constants::get_constant( 'WC_LOG_DIR' ) ) ) . '*.log' ); + $this->assertCount( 1, $files ); + + $file->delete(); + + $files = glob( trailingslashit( realpath( Constants::get_constant( 'WC_LOG_DIR' ) ) ) . '*.log' ); + $this->assertCount( 0, $files ); + } +}