From 6c56dcf4395798efb6e1fdcccc9008a9328c5342 Mon Sep 17 00:00:00 2001 From: Rene Reimann Date: Fri, 22 Jan 2016 09:29:38 +0100 Subject: [PATCH] #41 attachment_integration - add cleanup after test for uploads folder --- inc/Import/Service/WpPostImporter.php | 16 +++++++++- .../Service/WpAttachmentImporterTest.php | 32 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/inc/Import/Service/WpPostImporter.php b/inc/Import/Service/WpPostImporter.php index 4a64bc2..caa8695 100644 --- a/inc/Import/Service/WpPostImporter.php +++ b/inc/Import/Service/WpPostImporter.php @@ -74,7 +74,14 @@ public function import_post( Type\ImportPostInterface $import_post ) { if( $import_post->origin_attachment_url() ){ - $local_id = wp_insert_post( $import_postdata, TRUE ); + /** + * @param array $import_postdata Arguments for inserting an attachment. @see wp_insert_post + * @param string origin_attachment_url() Filename. + * @param int $local_parent_id Parent post ID. + * + * @return int Attachment ID. + **/ + $local_id = wp_insert_attachment( $import_postdata, $import_post->origin_attachment_url(), $local_parent_id ); }else{ @@ -311,11 +318,18 @@ public function import_attachment( $attachment_id, Type\ImportPostInterface $imp file_put_contents( $upload['file'], $response['body'] ); + /** post_mime_type to the attachment */ + $update_attachment = wp_update_post( array( + 'ID' => $attachment_id, + 'post_mime_type' => $filetype['type'] ) + ); + // Generate the metadata for the attachment, and update the database record. $attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $upload['file'] ); wp_update_attachment_metadata( $attachment_id, $attachment_metadata ); + } diff --git a/tests/phpunit/WpIntegration/Import/Service/WpAttachmentImporterTest.php b/tests/phpunit/WpIntegration/Import/Service/WpAttachmentImporterTest.php index 5c90506..a3382c9 100644 --- a/tests/phpunit/WpIntegration/Import/Service/WpAttachmentImporterTest.php +++ b/tests/phpunit/WpIntegration/Import/Service/WpAttachmentImporterTest.php @@ -23,6 +23,30 @@ public function setUp() { } + + /** + * cleanup test uploads folder + */ + public function teardown(){ + + $wp_upload = wp_upload_dir(); + $wp_upload_subdir = array_reverse( array_filter( explode( '/', $wp_upload['subdir'] ) ) ); + + array_map('unlink', glob( $wp_upload['basedir'] . $wp_upload['subdir'] . '/*' ) ); + + foreach( $wp_upload_subdir as $i => $dir ){ + + $parent_folder = FALSE; + + if( $i == 0 ){ + $i++; + $parent_folder = $wp_upload_subdir[ $i ]; + } + rmdir( $wp_upload['basedir'] . '/'. $parent_folder . '/' . $dir ); + } + + } + /** * @group import */ @@ -53,11 +77,13 @@ public function test_import_post() { * Now define the behaviour of the mock object. Each of the specified * methods ( @see ImportPostInterface ) should return a proper value! */ + $origin_attachment_url = 'http://inpsyde.com/wp-content/themes/i/assets/img/logo.png'; + $postdata = array( - 'title' => 'Mocky test fight', + 'title' => $origin_attachment_url, 'origin_author_id' => 12, 'status' => 'draft', - 'guid' => 'mocky', + 'guid' => $origin_attachment_url, 'date' => ( new \DateTime( 'NOW' ) )->format( 'Y-m-d H:i:s' ), 'comment_status' => 'open', 'ping_status' => 'open', @@ -71,7 +97,7 @@ public function test_import_post() { 'origin_link' => 'http://wpml2mlp.test/mocky', 'terms' => array( $term_mock ), 'meta' => array( $postmeta_mock_single, $postmeta_mock_array ), - 'origin_attachment_url' => 'http://inpsyde.com/wp-content/themes/i/assets/img/logo.png' + 'origin_attachment_url' => $origin_attachment_url ); $new_parent_id = 15;