Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions jekyll-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ function get_posts() {
}

$posts = array();
$post_types = apply_filters( 'jekyll_export_post_types', array( 'post', 'page' ) );
$post_types = apply_filters( 'jekyll_export_post_types', array( 'post', 'page', 'revision' ) );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this return all revisions (including those for published posts) or only revisions for unpublished posts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all revisions, but I don't think it's a bad thing. To my mind, if WP deemed something important enough to keep, so should the exporter (why risk missing something.. one could realize it too late after one has deleted one's original WP DB). It's just going to sit in the _drafts folder so no harm done anyway, worst case simply delete what you don't want from there after the export...


/**
* WordPress style rules don't let us interpolate a string before passing it to
* $wpdb->prepare, but I can't find any other way to do an "IN" query
* So query each post_type individually and merge the IDs
*/
foreach ( $post_types as $post_type ) {
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = %s", $post_type ) );
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", $post_type ) );
$posts = array_merge( $posts, $ids );
}

Expand Down Expand Up @@ -323,6 +323,7 @@ function init_temp_dir() {

$wp_filesystem->mkdir( $this->dir );
$wp_filesystem->mkdir( $this->dir . '_posts/' );
$wp_filesystem->mkdir( $this->dir . '_drafts/' );
$wp_filesystem->mkdir( $this->dir . 'wp-content/' );
}

Expand Down Expand Up @@ -400,15 +401,16 @@ function write( $output, $post ) {

global $wp_filesystem;

if ( get_post_type( $post ) === 'page' ) {
$wp_filesystem->mkdir( $this->dir . get_page_uri( $post->id ) );
if ( get_post_status( $post ) !== 'publish' ) {
$filename = '_drafts/' . sanitize_file_name( get_page_uri( $post->id ) . '-' . ( get_the_title( $post->id ) ) . '.md' );
} elseif ( get_post_type( $post ) === 'page' ) {
$filename = get_page_uri( $post->id ) . '.md';
} else {
$filename = '_' . get_post_type( $post ) . 's/' . date( 'Y-m-d', strtotime( $post->post_date ) ) . '-' . $post->post_name . '.md';
$filename = '_' . get_post_type( $post ) . 's/' . date( 'Y-m-d', strtotime( $post->post_date ) ) . '-' . sanitize_file_name( $post->post_name ) . '.md';
}

$wp_filesystem->mkdir( $this->dir . dirname( $filename ) );
$wp_filesystem->put_contents( $this->dir . $filename, $output );

}

/**
Expand Down
3 changes: 3 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

set -e

echo "Verifying mysql is up..."
pgrep mysql > /dev/null || sudo service mysql start

export PATH="$HOME/.composer/vendor/bin:./bin:$PATH"

echo "Running unit tests..."
Expand Down
109 changes: 78 additions & 31 deletions tests/test-wordpress-to-jekyll-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,45 @@
* @link https://github.com/benbalter/wordpress-to-jekyll-exporter/
*/

use Alchemy\Zippy\Zippy;

/**
* Test suite for JekyllExport
*/
/**
* Test suite for JekyllExport
*/
class WordPressToJekyllExporterTest extends WP_UnitTestCase {

/**
* Setup the test suite
* ID of sample post
*
* @var int
*/
function setUp() {
parent::setUp();
private static $post_id = 0;

/**
* ID of sample draft
*
* @var int
*/
private static $draft_id = 0;

/**
* ID of sample page
*
* @var int
*/
private static $page_id = 0;

/**
* ID of sample sub-page
*
* @var int
*/
private static $sub_page_id = 0;

/**
* Setup the test class
*/
static function setUpBeforeClass() {
parent::setUpBeforeClass();

$author = wp_insert_user(
array(
'user_login' => rand_str(),
Expand All @@ -35,7 +62,7 @@ function setUp() {
)
);

wp_insert_post(
self::$post_id = wp_insert_post(
array(
'post_name' => 'test-post',
'post_title' => 'Test Post',
Expand All @@ -48,7 +75,20 @@ function setUp() {
)
);

$page_id = wp_insert_post(
self::$draft_id = wp_insert_post(
array(
'post_name' => 'test-draft',
'post_title' => 'Test Draft',
'post_content' => 'This is a test <strong>draft</strong>.',
'post_status' => 'draft',
'post_author' => $author,
'post_category' => array( $category_id ),
'tags_input' => array( 'tag1', 'tag2' ),
'post_date' => '2014-01-01',
)
);

self::$page_id = wp_insert_post(
array(
'post_name' => 'test-page',
'post_title' => 'Test Page',
Expand All @@ -59,21 +99,27 @@ function setUp() {
)
);

wp_insert_post(
self::$sub_page_id = wp_insert_post(
array(
'post_name' => 'sub-page',
'post_title' => 'Sub Page',
'post_content' => 'This is a test <strong>sub</strong> page.',
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $page_id,
'post_parent' => self::$page_id,
'post_author' => $author,
)
);
}

/**
* Setup the test suite
*/
function setUp() {
parent::setUp();

global $jekyll_export;
$jekyll_export->init_temp_dir();

}

/**
Expand Down Expand Up @@ -108,16 +154,17 @@ function test_loads_dependencies() {
*/
function test_gets_post_ids() {
global $jekyll_export;
$this->assertEquals( 9, count( $jekyll_export->get_posts() ) );
$expected = array( self::$post_id, self::$draft_id, self::$page_id, self::$sub_page_id );
$actual = $jekyll_export->get_posts();
$this->assertTrue( ! array_diff( $expected, $actual ) && ! array_diff( $actual, $expected ) );
}

/**
* Test that it converts meta
*/
function test_convert_meta() {
global $jekyll_export;
$posts = $jekyll_export->get_posts();
$post = get_post( $posts[0] );
$post = get_post( self::$post_id );
$meta = $jekyll_export->convert_meta( $post );
$expected = array(
'id' => $post->ID,
Expand All @@ -137,9 +184,7 @@ function test_convert_meta() {
*/
function test_convert_terms() {
global $jekyll_export;
$posts = $jekyll_export->get_posts();
$post = get_post( $posts[0] );
$terms = $jekyll_export->convert_terms( $post->ID );
$terms = $jekyll_export->convert_terms( self::$post_id );
$this->assertEquals(
array(
0 => 'Testing',
Expand All @@ -158,8 +203,7 @@ function test_convert_terms() {
*/
function test_convert_content() {
global $jekyll_export;
$posts = $jekyll_export->get_posts();
$post = get_post( $posts[0] );
$post = get_post( self::$post_id );
$content = $jekyll_export->convert_content( $post );
$this->assertEquals( 'This is a test **post**.', $content );
}
Expand All @@ -178,25 +222,29 @@ function test_init_temp_dir() {
*/
function test_convert_posts() {
global $jekyll_export;
$posts = $jekyll_export->convert_posts();
$jekyll_export->convert_posts();

$post = $jekyll_export->dir . '/_posts/2014-01-01-test-post.md';

// write the file to the temp dir.
$this->assertTrue( file_exists( $post ) );
// write the post file to the temp dir.
$this->assertFileExists( $post );

// Handles pages.
$this->assertTrue( file_exists( $jekyll_export->dir . 'test-page.md' ) );
$this->assertTrue( file_exists( $jekyll_export->dir . 'test-page/sub-page.md' ) );
$this->assertFileExists( $jekyll_export->dir . 'test-page.md' );
$this->assertFileExists( $jekyll_export->dir . 'test-page/sub-page.md' );

// Handles drafts.
$this->assertFileExists( $jekyll_export->dir . '/_drafts/test-draft-Test-Draft.md' );

// writes the file contents.
$contents = file_get_contents( $post );
$this->assertContains( 'title: Test Post', $contents );
$this->assertContains( 'title: Test Post', $contents, 'file contents' );

// writes valid YAML.
$parts = explode( '---', $contents );
$this->assertEquals( 3,count( $parts ) );
$this->assertEquals( 3, count( $parts ), 'Invalid YAML Front Matter' );
$yaml = spyc_load( $parts[1] );
$this->assertNotEmpty( $yaml );
$this->assertNotEmpty( $yaml, 'Empty YAML' );

// writes the front matter.
$this->assertEquals( 'Test Post', $yaml['title'] );
Expand Down Expand Up @@ -246,8 +294,7 @@ function test_export_options() {
*/
function test_write() {
global $jekyll_export;
$posts = $jekyll_export->get_posts();
$post = get_post( $posts[0] );
$post = get_post( self::$post_id );
$jekyll_export->write( 'Foo', $post );
$post = $jekyll_export->dir . '/_posts/2014-01-01-test-post.md';
$this->assertTrue( file_exists( $post ) );
Expand Down