App-Dapper

 view release on metacpan or  search on metacpan

t/64-import-wordpress.t  view on Meta::CPAN

        . join('', @items) . "</channel>\n</rss>\n");
    return "$old/export.xml";
}

subtest 'a post, exactly' => sub {
    my $old = project();
    my $file = export($old, item(
        title => 'Hello &amp; welcome', link => 'https://old.example.com/2019/05/hello-world/', id => 1,
        date => '2019-05-01 10:30:00', slug => 'hello-world', status => 'publish', type => 'post',
        content => "First line\nsecond line\n\nAnother paragraph.", excerpt => "<p>A <b>short</b>\n hello.</p>",
        extra => [
            '<category domain="category" nicename="news"><![CDATA[News]]></category>',
            '<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>',
            '<category domain="category" nicename="perl"><![CDATA[Perl]]></category>',
            '<category domain="post_tag" nicename="hi"><![CDATA[hi]]></category>',
            '<category domain="post_tag" nicename="qa">Q&amp;A</category>',
            '<category domain="nav_menu" nicename="main"><![CDATA[Main menu]]></category>',
        ]));
    my $state = run_import('wordpress', $file);
    is(slurp('_source/blog/hello-world.html'), <<'HTML', 'the whole file');
---
title: Hello & welcome
date: 2019-05-01T10:30:00
layout: post
categories:
- News
- Perl
tags:
- hi
- Q&A
description: A short hello.
urlpattern: /2019/05/hello-world/
---
<p>First line<br>
second line</p>

<p>Another paragraph.</p>
HTML
    is_deeply($state->{pages}, [{ file => '_source/blog/hello-world.html', kind => 'post', draft => 0 }], 'the summary');
    is($state->{to}, 'blog', 'posts go in blog');
    is_deeply($state->{skipped}, [], 'nothing skipped');
    is_deeply($state->{notes}, {}, 'nothing to note');
};

subtest 'statuses and post types' => sub {
    my $old = project();
    my $file = export($old,
        item(title => 'Published', slug => 'published', status => 'publish', type => 'post', link => 'https://x.com/published/', content => 'x'),
        item(title => 'Draft', slug => 'draft', status => 'draft', type => 'post', link => 'https://x.com/draft/', content => 'x'),
        item(title => 'Pending', slug => 'pending', status => 'pending', type => 'post', link => 'https://x.com/pending/', content => 'x'),
        item(title => 'Private', slug => 'private', status => 'private', type => 'post', link => 'https://x.com/private/', content => 'x'),
        item(title => 'Future', slug => 'future', status => 'future', type => 'post', link => 'https://x.com/future/', content => 'x', date => '2999-01-01 00:00:00'),
        item(title => 'Trash', slug => 'trash', status => 'trash', type => 'post', content => 'x'),
        item(title => 'Auto', slug => 'auto', status => 'auto-draft', type => 'post', content => 'x'),
        item(title => 'Inherit', slug => 'inherit', status => 'inherit', type => 'post', content => 'x'),
        item(title => 'No status', slug => 'no-status', type => 'post', content => 'x'),
        item(title => 'No type', slug => 'no-type', status => 'publish', content => 'x'),
        item(title => 'A page', slug => 'a-page', status => 'publish', type => 'page', link => 'https://x.com/parent/a-page/', content => 'x'),
        item(title => 'Draft page', slug => 'draft-page', status => 'draft', type => 'page', content => 'x'),
        item(title => 'Photo', slug => 'photo', status => 'inherit', type => 'attachment'),
        item(title => 'Menu', slug => 'menu', status => 'publish', type => 'nav_menu_item'),
        item(title => 'Product', slug => 'product', status => 'publish', type => 'product', content => 'x'),
        item(title => 'Block', slug => 'block', status => 'publish', type => 'wp_block', content => 'x'),
    );
    my ($state, $out) = run_import('wordpress', $file, loud => 1);
    is_deeply([ map { "$_->{file} $_->{kind} $_->{draft}" } @{$state->{pages}} ], [
        '_source/blog/published.html post 0', '_source/blog/draft.html post 1', '_source/blog/pending.html post 1',
        '_source/blog/private.html post 1', '_source/blog/future.html post 0', '_source/blog/no-status.html post 0',
        '_source/blog/no-type.html post 0', '_source/a-page.html page 0', '_source/draft-page.html page 1',
    ], 'what was imported, in order');
    my $front = sub { (App::Dapper::Utils::split_frontmatter(slurp($_[0]), $_[0]))[0] };
    is($front->('_source/blog/draft.html')->{draft}, 1, 'a draft');
    ok(!exists $front->('_source/blog/draft.html')->{urlpattern}, 'with no address');
    ok(!exists $front->('_source/blog/published.html')->{draft}, 'published posts have no draft key');
    is($front->('_source/blog/future.html')->{urlpattern}, '/future/', 'a scheduled post keeps its address');
    is($front->('_source/blog/future.html')->{date}, '2999-01-01T00:00:00', 'and its date');
    is($front->('_source/a-page.html')->{urlpattern}, '/parent/a-page/', 'a page keeps its address');
    ok(!exists $front->('_source/a-page.html')->{layout}, 'pages get no layout');
    ok(!-e "_source/blog/$_.html", "no $_") for qw/ trash auto inherit photo menu product block /;
    is($out, qq{Imported 7 posts and 2 pages into _source (4 as drafts).\nRun "dapper check" to find links that need fixing.\n}, 'the report');
};

subtest 'slugs and addresses' => sub {
    my $old = project();
    my $file = export($old,
        item(title => 'No Slug: Here!', slug => '', id => 5, status => 'publish', type => 'post', link => 'https://x.com/?p=5', content => 'x'),
        item(title => '', slug => '', id => 6, status => 'publish', type => 'post', content => 'x'),
        item(title => '!!!', status => 'publish', type => 'post', content => 'x'),
        item(slug => 'untitled', status => 'publish', type => 'post', content => 'x'),
        item(title => 'Page id', slug => 'page-id', status => 'publish', type => 'page', link => 'https://x.com/?page_id=9', content => 'x'),
        item(title => 'Query', slug => 'query', status => 'publish', type => 'post', link => 'https://x.com/2020/query/?utm_source=x#c', content => 'x'),
        item(title => 'Bare', slug => 'bare', status => 'publish', type => 'post', link => 'https://x.com', content => 'x'),
        item(title => 'Relative', slug => 'relative', status => 'publish', type => 'post', link => 'relative/path/', content => 'x'),
        item(title => 'Empty link', slug => 'empty-link', status => 'publish', type => 'post', link => '', content => 'x'),
        item(title => 'Same', slug => 'same', status => 'publish', type => 'post', link => 'https://x.com/a/', content => 'one'),
        item(title => 'Same', slug => 'same', status => 'publish', type => 'post', link => 'https://x.com/b/', content => 'two'),
        item(title => 'Same', slug => 'same', status => 'publish', type => 'post', link => 'https://x.com/c/', content => 'three'),
        item(title => 'Same page', slug => 'same', status => 'publish', type => 'page', content => 'page'),
    );
    my $state = run_import('wordpress', $file);
    my @files = map { $_->{file} } @{$state->{pages}};
    is_deeply(\@files, [ map { "_source/$_" } qw( blog/no-slug-here.html blog/post-6.html blog/post-2.html blog/untitled.html page-id.html
        blog/query.html blog/bare.html blog/relative.html blog/empty-link.html blog/same.html blog/same-2.html blog/same-3.html same.html ) ], 'file names');
    my $front = sub { (App::Dapper::Utils::split_frontmatter(slurp($_[0]), $_[0]))[0] };
    ok(!exists $front->('_source/blog/no-slug-here.html')->{urlpattern}, '?p= addresses are not kept');
    ok(!exists $front->('_source/page-id.html')->{urlpattern}, '?page_id= addresses are not kept');
    is($front->('_source/blog/query.html')->{urlpattern}, '/2020/query/', 'query and fragment removed');
    is($front->('_source/blog/bare.html')->{urlpattern}, '/', 'the home page address');
    is($front->('_source/blog/relative.html')->{urlpattern}, '/relative/path/', 'a relative address gets a slash');
    ok(!exists $front->('_source/blog/empty-link.html')->{urlpattern}, 'an empty link');
    ok(!exists $front->('_source/blog/post-6.html')->{title}, 'an empty title is left out');
    is($front->('_source/blog/untitled.html')->{title}, undef, 'no title at all');
    like(slurp('_source/blog/same-2.html'), qr{<p>two</p>}, 'the second of the same name');
    like(slurp('_source/blog/same-3.html'), qr{<p>three</p>}, 'the third');
};

subtest 'entities, CDATA and dates' => sub {
    my $old = project();
    my $file = export($old,
        item(title => 'Q&amp;A: &lt;tags&gt; &#8217;quotes&#8217; &#x2014; &quot;x&quot; &#39;y&#39; &apos;z&apos; &amp;lt;', slug => 'entities',
             status => 'publish', type => 'post', date => '0000-00-00 00:00:00', content => 'x',



( run in 0.668 second using v1.01-cache-2.11-cpan-60884a8df76 )