Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
#41 attachment_integration - add cleanup after test for uploads folder
Browse files Browse the repository at this point in the history
  • Loading branch information
derpixler committed Jan 22, 2016
1 parent 850a5d9 commit 6c56dcf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
16 changes: 15 additions & 1 deletion inc/Import/Service/WpPostImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand Down Expand Up @@ -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 );



}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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',
Expand All @@ -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;
Expand Down

0 comments on commit 6c56dcf

Please sign in to comment.