diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 8b27cb7b7..e69a03faf 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -144,7 +144,7 @@ class Item extends Entity { return apply_filters("tainacan-item-get-thumbnail", $thumbs, $this); } - private function get_image_blurhash($file_path, $width, $height) { + public function get_image_blurhash($file_path, $width, $height) { if ( !(version_compare(PHP_VERSION, '7.2.0') >= 0) || !$image = @imagecreatefromstring(file_get_contents($file_path)) diff --git a/tests/attachment/tainacan.jpg b/tests/attachment/tainacan.jpg new file mode 100644 index 000000000..88c96831b Binary files /dev/null and b/tests/attachment/tainacan.jpg differ diff --git a/tests/test-api-items.php b/tests/test-api-items.php index 8c83cb29f..a3025f2bc 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -1047,6 +1047,70 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { } + /* + * @group Documents + */ + function test_item_get_thumbnail() { + $TainacanMedia = \Tainacan\Media::get_instance(); + $ItemsRepo = \Tainacan\Repositories\Items::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'collectionComments', + 'allow_comments' => 'closed' + ), + true, + true + ); + $item = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'itemComments1', + 'collection' => $collection, + ), + true, + true + ); + + $orig_file = './tests/attachment/tainacan.jpg'; + $test_file = '/tmp/tainacan.jpg'; + copy( $orig_file, $test_file ); + $request = new \WP_REST_Request( 'POST', '/wp/v2/media' ); + $request->set_header( 'Content-Type', 'image/jpeg' ); + $request->set_header( 'Content-Disposition', 'attachment; filename=codeispoetrywp.jpg' ); + $request->set_param( 'post', $item->get_id() ); + + global $TAINACAN_UPLOADING_ATTACHMENT_TO_POST; + $TAINACAN_UPLOADING_ATTACHMENT_TO_POST = $item->get_id(); + + $request->set_body( file_get_contents( $test_file ) ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset($TAINACAN_UPLOADING_ATTACHMENT_TO_POST); + + $this->assertEquals( 201, $response->get_status() ); + $attachment = get_post( $data['id'] ); + + $item->set_document( $attachment->ID ); + $item->set_document_type( 'attachment' ); + + if( $item->validate() ) { + $item = $ItemsRepo->update($item); + } + + $thumb_id = $ItemsRepo->get_thumbnail_id_from_document($item); + if (!is_null($thumb_id)) { + set_post_thumbnail( $item->get_id(), (int) $thumb_id ); + } + + $thumbs = $item->get_thumbnail(); + foreach ( $thumbs as $thumb ) { + if (is_array($thumb) && count($thumb) == 5) { + $this->assertContains($thumb[4], ['V4P?:h00Rj~qM{of%MRjWBRjD%%MRjayofj[%M-;RjRj', 'LATI:i~qNG~W~qNGxaNGM|xaNGxa']); + } + } + } } ?> diff --git a/tests/test-items.php b/tests/test-items.php index 109d2c695..8cae8b3df 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -516,4 +516,13 @@ class Items extends TAINACAN_UnitTestCase { } + function test_item_blurhash() { + $item = new Entities\Item(); + $orig_file = './tests/attachment/tainacan.jpg'; + $test_file = '/tmp/tainacan.jpg'; + copy( $orig_file, $test_file ); + $blurhash = $item->get_image_blurhash($test_file, 40,40); + $this->assertContains($blurhash, ['V4P?:h00Rj~qM{of%MRjWBRjD%%MRjayofj[%M-;RjRj', 'LATI:i~qNG~W~qNGxaNGM|xaNGxa']); + } + }