From 8b832c8803f069710271249b5db7f88f4343755d Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 20 Jun 2024 10:53:12 +0200 Subject: [PATCH 1/4] ContentImporter test started --- includes/ContentImport/Service.php | 1 - .../ContentImport/TestContentImporter.php | 33 +++++++++++++++++++ tests/phpunit/ContentImport/TestService.php | 6 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/ContentImport/TestContentImporter.php diff --git a/includes/ContentImport/Service.php b/includes/ContentImport/Service.php index 4685571a..4cfe77d7 100644 --- a/includes/ContentImport/Service.php +++ b/includes/ContentImport/Service.php @@ -3,7 +3,6 @@ namespace lloc\Msls\ContentImport; use lloc\Msls\ContentImport\LogWriters\AdminNoticeLogger; -use lloc\Msls\MslsOptions; use lloc\Msls\MslsRegistryInstance; /** diff --git a/tests/phpunit/ContentImport/TestContentImporter.php b/tests/phpunit/ContentImport/TestContentImporter.php new file mode 100644 index 00000000..4fd49781 --- /dev/null +++ b/tests/phpunit/ContentImport/TestContentImporter.php @@ -0,0 +1,33 @@ +test = new ContentImporter( $main ); + } + + public function test_logger(): void { + $this->test->set_logger( \Mockery::mock( ImportLogger::class ) ); + + $this->assertInstanceOf( ImportLogger::class, $this->test->get_logger() ); + } + + public function test_relations(): void { + $this->test->set_relations( \Mockery::mock( Relations::class ) ); + + $this->assertInstanceOf( Relations::class, $this->test->get_relations() ); + } +} diff --git a/tests/phpunit/ContentImport/TestService.php b/tests/phpunit/ContentImport/TestService.php index e67cfadc..76f49daf 100644 --- a/tests/phpunit/ContentImport/TestService.php +++ b/tests/phpunit/ContentImport/TestService.php @@ -10,7 +10,8 @@ class TestService extends MslsUnitTestCase { public function test_register_not_active_false(): void { - $options = \Mockery::mock( MslsOptions::class ); + $options = \Mockery::mock( MslsOptions::class ); + $options->activate_content_import = false; Functions\expect( 'msls_options' )->once()->andReturn( $options ); @@ -19,7 +20,8 @@ public function test_register_not_active_false(): void { } public function test_register_active_true(): void { - $options = \Mockery::mock( MslsOptions::class ); + $options = \Mockery::mock( MslsOptions::class ); + $options->activate_content_import = true; Functions\expect( 'msls_options' )->once()->andReturn( $options ); From 3799cfabf491f9a3fa01eeedb4daec73e9e79068 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 20 Jun 2024 11:34:48 +0200 Subject: [PATCH 2/4] Tests added --- .../ContentImport/TestImportLogger.php | 36 +++++++++++++++++++ tests/phpunit/ContentImport/TestRelations.php | 22 ++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/phpunit/ContentImport/TestImportLogger.php create mode 100644 tests/phpunit/ContentImport/TestRelations.php diff --git a/tests/phpunit/ContentImport/TestImportLogger.php b/tests/phpunit/ContentImport/TestImportLogger.php new file mode 100644 index 00000000..7df163a4 --- /dev/null +++ b/tests/phpunit/ContentImport/TestImportLogger.php @@ -0,0 +1,36 @@ +test = new ImportLogger( $coordinates ); + } + + public function provider_get_data(): array { + return array( + array( 'info' ), + array( 'error' ), + array( 'success' ), + ); + } + + /** + * @dataProvider provider_get_data + */ + public function test_get_data( $key ): void { + $this->assertArrayHasKey( $key, $this->test->get_data() ); + } +} diff --git a/tests/phpunit/ContentImport/TestRelations.php b/tests/phpunit/ContentImport/TestRelations.php new file mode 100644 index 00000000..0ccc81d9 --- /dev/null +++ b/tests/phpunit/ContentImport/TestRelations.php @@ -0,0 +1,22 @@ +test = new Relations( $coordinates ); + } + + public function test_get_data(): void { + $this->assertIsArray( $this->test->get_data() ); + } +} From 97cfc7a6f9f81b2793220e95037d2f84120ae696 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Fri, 21 Jun 2024 10:48:02 +0200 Subject: [PATCH 3/4] Escaping checked --- includes/Component/Input/Checkbox.php | 12 +++++++----- includes/Component/Input/Select.php | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/Component/Input/Checkbox.php b/includes/Component/Input/Checkbox.php index 20a625f0..9dfbb758 100644 --- a/includes/Component/Input/Checkbox.php +++ b/includes/Component/Input/Checkbox.php @@ -6,6 +6,7 @@ /** * Class Checkbox + * * @package lloc\Msls\Component\Input */ class Checkbox implements InputInterface { @@ -25,7 +26,7 @@ class Checkbox implements InputInterface { * @param string $value */ public function __construct( string $key, ?string $value ) { - $this->key = esc_attr( $key ); + $this->key = esc_attr( $key ); $this->selected = checked( 1, $value, false ); } @@ -33,9 +34,10 @@ public function __construct( string $key, ?string $value ) { * @return string */ public function render(): string { - return sprintf( '', + return sprintf( + '', $this->key, - $this->selected ); + $this->selected + ); } - -} \ No newline at end of file +} diff --git a/includes/Component/Input/Select.php b/includes/Component/Input/Select.php index 1d47bff4..99788ab3 100644 --- a/includes/Component/Input/Select.php +++ b/includes/Component/Input/Select.php @@ -36,6 +36,6 @@ public function __construct( string $key, array $arr, ?string $selected = null ) public function render(): string { $name = apply_filters( 'msls_input_select_name', 'msls[' . $this->key . ']' ); - return sprintf( '', $this->key, $name, $this->options->render() ); + return sprintf( '', $this->key, esc_attr( $name ), $this->options->render() ); } } From 82bffb7af8e30f785f7cb9555625b693f4abe146 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sun, 23 Jun 2024 14:49:46 +0200 Subject: [PATCH 4/4] Tests added --- .../TestAttachmentPathFinder.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/phpunit/ContentImport/TestAttachmentPathFinder.php diff --git a/tests/phpunit/ContentImport/TestAttachmentPathFinder.php b/tests/phpunit/ContentImport/TestAttachmentPathFinder.php new file mode 100644 index 00000000..184c816b --- /dev/null +++ b/tests/phpunit/ContentImport/TestAttachmentPathFinder.php @@ -0,0 +1,47 @@ +test = new AttachmentPathFinder(); + } + + public function dataprovider_filter_srcset() { + $image_src = 'http://example.com/image.jpg'; + $msls_imported = array( + 'blog' => 1, + 'post' => 1, + ); + $source_post = (object) array( 'guid' => 'http://example.com/image.jpg' ); + + return array( + array( array(), $image_src, 0, array() ), + array( array(), $image_src, '', array() ), + array( array(), $image_src, null, array() ), + array( array(), $image_src, 1, array(), null, 1, 1 ), + array( array(), $image_src, 1, array(), array( 'random' => 'item' ), 1, 1 ), + array( array( array( 'url' => $image_src ) ), $image_src, 1, array( array( 'url' => $image_src ) ), $msls_imported, 1, 0, 1 ), + array( array( array( 'url' => $image_src ) ), $image_src, 1, array( array( 'url' => $image_src ) ), $msls_imported, 1, 0, 1, $source_post ), + array( array( array( 'url' => 'http://example.com/image-300x300.jpg' ) ), $image_src, 1, array( array( 'url' => 'http://example.com/image-300x300.jpg' ) ), $msls_imported, 1, 0, 1, $source_post ), + ); + } + + /** + * @dataProvider dataprovider_filter_srcset + */ + public function test_filter_srcset( $source, $imageSrc, $attachmentId, $expected, $msls_imported = null, $times_gpm = 0, $time_dpm = 0, $times_gbp = 0, $blog_post = false ) { + Functions\expect( 'get_post_meta' )->times( $times_gpm )->andReturn( $msls_imported ); + Functions\expect( 'delete_post_meta' )->times( $time_dpm ); + Functions\expect( 'get_blog_post' )->times( $times_gbp )->andReturn( $blog_post ); + + $this->assertEquals( $expected, $this->test->filter_srcset( $source, null, $imageSrc, null, $attachmentId ) ); + } +}