Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 81 lines (67 sloc) 2.375 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#!/usr/bin/perl

use strict;
use warnings;

use lib 't/lib', 'lib', 'extlib';
use Test::More tests => 7;

BEGIN {
        $ENV{MT_APP} = 'MT::App::CMS';
}

use MT;
use MT::Author;
use MT::CMS::Export;
use MT::Blog;
use MT::Comment;
use MT::Entry;
use MT::Import;
use MT::ImportExport;
use MT::Test qw( :app :db :data );

my $blog = MT::Blog->load(1);
my $user = MT::Author->load(2);

# export entries from a valid blog
my $app = _run_app( 'MT::App::CMS', { __test_user => $user, __mode => 'export', blog_id => $blog->id } );
my $good_out = delete $app->{__test_output};
ok ($good_out, "Export data is present");

# export entries for an invalid blog
eval {
    $app = _run_app( 'MT::App::CMS', { __test_user => $user, __mode => 'export', blog_id => 1000 } );
};
my $eval_error = $@;
ok ( $eval_error );
like ($eval_error, qr/Invalid request/i, "Failed as expected");

# write the file and make sure no funny characters are there
open OUT, ">test_import.txt";
foreach my $line (split "\n", $good_out) {
chomp $line;
print OUT "$line\n";
}
close OUT;

# we are going to use the existing blog, so make sure all entries and comments are removed
my @entries = MT::Entry->load({ blog_id => $blog->id });
foreach my $entry (@entries) {
$entry->remove;
}
ok (MT::Entry->count({ blog_id => $blog->id }) == 0, "Got rid of all entries");

my @comments = MT::Comment->load({ blog_id => $blog->id });
foreach my $comment (@comments) {
$comment->remove;
}
ok (MT::Comment->count({ blog_id => $blog->id }) == 0, "Got rid of all comments");

# use the file as the input to the import script
my $impt = MT::Import->new;
my $ie = MT::ImportExport->new;
open IN, "<test_import.txt" or die "COULD NOT OPEN FILE";
close IN;
my $iter = $impt->_get_stream_iterator('test_import.txt', sub { my ($string) = @_; print STDERR "[cb_iterator] $string\n"; 1});

my %param = ();
$param{'Iter'} = $iter;
$param{'Blog'} = $blog;
$param{'Callback'} = 1;
$param{'ParentAuthor'} = $user;
$param{'NewAuthorPassword'} = 'PASSWORD';
$param{'ConvertBreaks'} = '';
$param{'Callback'} = sub { my ($string) = @_; print " $string\n"; 1};
my $result = $ie->import_contents(%param);

# check if the blog has entries and comments
ok (MT::Entry->count({ blog_id => $blog->id }) > 0, "We have entries again");
ok (MT::Comment->count({ blog_id => $blog->id }) > 0, "We have comments again");

unlink("test_import.txt");
Something went wrong with that request. Please try again.