| 1 | #!/usr/bin/perl |
|---|
| 2 | # $Id: 27-context.t 1100 2007-12-12 01:48:53Z hachi $ |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use lib 't/lib'; |
|---|
| 7 | use lib 'lib'; |
|---|
| 8 | use lib 'extlib'; |
|---|
| 9 | |
|---|
| 10 | use Test::More tests => 55; |
|---|
| 11 | use File::Temp qw( tempfile ); |
|---|
| 12 | |
|---|
| 13 | use MT; |
|---|
| 14 | use MT::Author; |
|---|
| 15 | use MT::Blog; |
|---|
| 16 | use MT::Comment; |
|---|
| 17 | use MT::Entry; |
|---|
| 18 | use MT::Template; |
|---|
| 19 | use MT::Template::Context; |
|---|
| 20 | use MT::Util qw( first_n_words html_text_transform ); |
|---|
| 21 | |
|---|
| 22 | use vars qw( $DB_DIR $T_CFG $BASE ); |
|---|
| 23 | |
|---|
| 24 | use MT::Test qw(:db :data); |
|---|
| 25 | |
|---|
| 26 | my $mt = MT->new( Config => $T_CFG ) or die MT->errstr; |
|---|
| 27 | isa_ok($mt, 'MT'); |
|---|
| 28 | |
|---|
| 29 | sub build { |
|---|
| 30 | # my($ctx, $markup) = @_; |
|---|
| 31 | # my $b = MT::Builder->new; |
|---|
| 32 | # my $tokens = $b->compile($ctx, $markup) or die $b->errstr; |
|---|
| 33 | # $b->build($ctx, $tokens); |
|---|
| 34 | |
|---|
| 35 | my($ctx, $markup) = @_; |
|---|
| 36 | my $b = $ctx->stash('builder'); |
|---|
| 37 | my $tokens = $b->compile($ctx, $markup); |
|---|
| 38 | print('# -- error compiling: ' . $b->errstr), return undef |
|---|
| 39 | unless defined $tokens; |
|---|
| 40 | my $res = $b->build($ctx, $tokens); |
|---|
| 41 | print '# -- error building: ' . ($b->errstr ? $b->errstr : '') . "\n" |
|---|
| 42 | unless defined $res; |
|---|
| 43 | return $res; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | ## Need to test: |
|---|
| 47 | ## create a couple of entries |
|---|
| 48 | ## MTEntries: |
|---|
| 49 | ## test all arguments (days, lastn, category) |
|---|
| 50 | ## test DateHeader section |
|---|
| 51 | ## test conditionals for extended entries and allow comments |
|---|
| 52 | ## test that we can do stash->('entries', \@entries) to set entrylist |
|---|
| 53 | ## test sort_order_posts works |
|---|
| 54 | ## test that only status=LIVE posts get published |
|---|
| 55 | ## test Category tag |
|---|
| 56 | ## test all MTEntry* tags |
|---|
| 57 | ## MTComments; |
|---|
| 58 | ## test sort_order_comments works |
|---|
| 59 | ## test all MTComment* tags |
|---|
| 60 | ## MTArchiveList: |
|---|
| 61 | ## test all archive types |
|---|
| 62 | ## test MTEntries inside |
|---|
| 63 | ## test MTArchiveTitle correctness for all archive types |
|---|
| 64 | ## test all MTArchive* tags |
|---|
| 65 | ## test each of the tags, both for failure (out of context) and success |
|---|
| 66 | |
|---|
| 67 | my $blog = MT::Blog->load(1); |
|---|
| 68 | isa_ok($blog, 'MT::Blog'); |
|---|
| 69 | |
|---|
| 70 | my $author = MT::Author->load({ name => 'Chuck D' }); |
|---|
| 71 | isa_ok($author, 'MT::Author'); |
|---|
| 72 | |
|---|
| 73 | ok($author && $author->is_valid_password('bass'), 'valid'); |
|---|
| 74 | ok($author && !$author->is_valid_password('wrong'), 'invalid'); |
|---|
| 75 | |
|---|
| 76 | my $ctx = MT::Template::Context->new; |
|---|
| 77 | isa_ok($ctx, 'MT::Template::Context'); |
|---|
| 78 | $ctx->stash('blog', $blog); |
|---|
| 79 | $ctx->stash('blog_id', $blog->id); |
|---|
| 80 | $ctx->stash('builder', MT::Builder->new); |
|---|
| 81 | isa_ok($ctx, 'MT::Template::Context'); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | ## Test MTBlog* tags |
|---|
| 85 | is(build($ctx, '<$MTBlogName$>'), $blog->name, 'MTBlogName'); |
|---|
| 86 | is(build($ctx, '<$MTBlogURL$>'), $blog->site_url, 'MTBlogURL'); |
|---|
| 87 | is(build($ctx, '<$MTBlogDescription$>'), $blog->description, 'MTBlogDescription'); |
|---|
| 88 | is(build($ctx, '<a href="<$MTBlogURL$>"><$MTBlogName$></a>'), |
|---|
| 89 | qq(<a href="@{[ $blog->site_url ]}">@{[ $blog->name ]}</a>), 'href'); |
|---|
| 90 | |
|---|
| 91 | my @cmts = MT::Comment->load({entry_id => 1}); |
|---|
| 92 | for (@cmts) { $_->remove() } |
|---|
| 93 | my $entry = MT::Entry->load(1); |
|---|
| 94 | $entry->text_more("Something\nelse"); |
|---|
| 95 | $ctx->stash('entry', $entry); |
|---|
| 96 | my $ts = local $ctx->{current_timestamp} = $entry->created_on; |
|---|
| 97 | my @ts = unpack 'A4A2A2A2A2A2', $ts; |
|---|
| 98 | $ts = sprintf "%04d.%02d.%02d %02d:%02d:%02d", @ts; |
|---|
| 99 | |
|---|
| 100 | is(build($ctx, '<$MTEntryTitle$>'), $entry->title, 'MTEntryTitle'); |
|---|
| 101 | is(build($ctx, '<$MTEntryAuthor$>'), $entry->author->name, 'MTEntryAuthor'); |
|---|
| 102 | is(build($ctx, '<$MTEntryMore$>'), html_text_transform($entry->text_more), 'MTEntryMore'); |
|---|
| 103 | is(build($ctx, '<$MTEntryCommentCount$>'), 0, 'MTEntryCommentCount'); |
|---|
| 104 | is(build($ctx, '<$MTEntryDate format="%Y.%m.%d %H:%M:%S"$>'), $ts, 'MTEntryDate format'); |
|---|
| 105 | |
|---|
| 106 | is(build($ctx, '<$MTEntryBody words="2"$>'), first_n_words($entry->text, 2), 'MTEntryBody words 2'); |
|---|
| 107 | |
|---|
| 108 | ## Test convert_breaks variations with <$MTEntryBody$>. |
|---|
| 109 | is(build($ctx, '<$MTEntryBody$>'), html_text_transform($entry->text), 'MTEntryBody'); |
|---|
| 110 | is(build($ctx, '<$MTEntryBody convert_breaks="0"$>'), $entry->text, 'convert_breaks 0'); |
|---|
| 111 | is(build($ctx, '<$MTEntryBody convert_breaks="1"$>'), html_text_transform($entry->text), 'convert_breaks 1'); |
|---|
| 112 | $entry->convert_breaks(0); |
|---|
| 113 | is(build($ctx, '<$MTEntryBody$>'), $entry->text, 'MTEntryBody'); |
|---|
| 114 | is(build($ctx, '<$MTEntryBody convert_breaks="0"$>'), $entry->text, 'convert_breaks 0'); |
|---|
| 115 | is(build($ctx, '<$MTEntryBody convert_breaks="1"$>'), html_text_transform($entry->text), 'convert_breaks 1'); |
|---|
| 116 | $entry->column_values->{convert_breaks} = undef; |
|---|
| 117 | is(build($ctx, '<$MTEntryBody$>'), html_text_transform($entry->text), 'MTEntryBody'); |
|---|
| 118 | is(build($ctx, '<$MTEntryBody convert_breaks="0"$>'), $entry->text, 'convert_breaks 0'); |
|---|
| 119 | is(build($ctx, '<$MTEntryBody convert_breaks="1"$>'), html_text_transform($entry->text), 'convert_breaks 1'); |
|---|
| 120 | $blog->convert_paras(0); |
|---|
| 121 | is(build($ctx, '<$MTEntryBody$>'), $entry->text, 'MTEntryBody'); |
|---|
| 122 | is(build($ctx, '<$MTEntryBody convert_breaks="0"$>'), $entry->text, 'convert_breaks 0'); |
|---|
| 123 | is(build($ctx, '<$MTEntryBody convert_breaks="1"$>'), html_text_transform($entry->text), 'convert_breaks 1'); |
|---|
| 124 | $entry->convert_breaks(1); |
|---|
| 125 | $blog->convert_paras(1); |
|---|
| 126 | |
|---|
| 127 | ## Test convert_breaks variations with <$MTEntryMore$>. |
|---|
| 128 | is(build($ctx, '<$MTEntryMore$>'), html_text_transform($entry->text_more), 'MTEntryMore'); |
|---|
| 129 | is(build($ctx, '<$MTEntryMore convert_breaks="0"$>'), $entry->text_more, 'convert_breaks 0'); |
|---|
| 130 | is(build($ctx, '<$MTEntryMore convert_breaks="1"$>'), html_text_transform($entry->text_more), 'convert_breaks 1'); |
|---|
| 131 | $entry->convert_breaks(0); |
|---|
| 132 | is(build($ctx, '<$MTEntryMore$>'), $entry->text_more, 'MTEntryMore'); |
|---|
| 133 | is(build($ctx, '<$MTEntryMore convert_breaks="0"$>'), $entry->text_more, 'convert_breaks 0'); |
|---|
| 134 | is(build($ctx, '<$MTEntryMore convert_breaks="1"$>'), html_text_transform($entry->text_more), 'convert_breaks 1'); |
|---|
| 135 | $entry->column_values->{convert_breaks} = undef; |
|---|
| 136 | is(build($ctx, '<$MTEntryMore$>'), html_text_transform($entry->text_more), 'MTEntryMore'); |
|---|
| 137 | is(build($ctx, '<$MTEntryMore convert_breaks="0"$>'), $entry->text_more, 'convert_breaks 0'); |
|---|
| 138 | is(build($ctx, '<$MTEntryMore convert_breaks="1"$>'), html_text_transform($entry->text_more), 'convert_breaks 1'); |
|---|
| 139 | $blog->convert_paras(0); |
|---|
| 140 | is(build($ctx, '<$MTEntryMore$>'), $entry->text_more, 'MTEntryMore'); |
|---|
| 141 | is(build($ctx, '<$MTEntryMore convert_breaks="0"$>'), $entry->text_more, 'convert_breaks 0'); |
|---|
| 142 | is(build($ctx, '<$MTEntryMore convert_breaks="1"$>'), html_text_transform($entry->text_more), 'convert_breaks 1'); |
|---|
| 143 | $entry->convert_breaks(1); |
|---|
| 144 | $blog->convert_paras(1); |
|---|
| 145 | |
|---|
| 146 | ## This should run remove_html first (stripping "<html">), then |
|---|
| 147 | ## run encode_xml. Previous to 2.61 encode_xml was run first, wrapping |
|---|
| 148 | ## the excerpt in CDATA, which was then screwed up by remove_html. |
|---|
| 149 | $entry->excerpt('Contains <html>'); |
|---|
| 150 | is(build($ctx, '<$MTEntryExcerpt remove_html="1" encode_xml="1"$>'), 'Contains ', 'remove_html 1 encode_xml 1'); |
|---|
| 151 | $entry->excerpt('Fight the powers that be'); |
|---|
| 152 | |
|---|
| 153 | ## Test with set excerpt. |
|---|
| 154 | is(build($ctx, '<$MTEntryExcerpt$>'), $entry->excerpt, 'MTEntryExcerpt'); |
|---|
| 155 | is(build($ctx, '<$MTEntryExcerpt convert_breaks="1"$>'), html_text_transform($entry->excerpt), 'convert_breaks 1'); |
|---|
| 156 | is(build($ctx, '<$MTEntryExcerpt no_generate="1"$>'), $entry->excerpt, 'no_generate 1'); |
|---|
| 157 | |
|---|
| 158 | ## Test with auto-generating excerpt. |
|---|
| 159 | $entry->excerpt(''); |
|---|
| 160 | is(build($ctx, '<$MTEntryExcerpt$>'), first_n_words($entry->text, 40) . '...', 'MTEntryExcerpt'); |
|---|
| 161 | is(build($ctx, '<$MTEntryExcerpt convert_breaks="1"$>'), first_n_words($entry->text, 40) . '...', 'convert_breaks 1'); |
|---|
| 162 | is(build($ctx, '<$MTEntryExcerpt no_generate="1"$>'), '', 'no_generate 1'); |
|---|
| 163 | |
|---|
| 164 | ## Make sure text formatting is applied before excerpt is generated, |
|---|
| 165 | ## unless convert_breaks="0" is explicitly set. |
|---|
| 166 | ## Change to use Markdown |
|---|
| 167 | $entry->convert_breaks('markdown'); |
|---|
| 168 | $entry->text(q(This text is **strong**)); |
|---|
| 169 | is(build($ctx, '<$MTEntryExcerpt$>'), q(This text is strong...), 'MTEntryExcerpt'); |
|---|
| 170 | is(build($ctx, '<$MTEntryExcerpt convert_breaks="1"$>'), q(This text is strong...), 'convert_breaks 1'); |
|---|
| 171 | is(build($ctx, '<$MTEntryExcerpt convert_breaks="0"$>'), q(This text is **strong**...), 'convert_breaks 0'); |
|---|
| 172 | |
|---|
| 173 | $entry->convert_breaks('__default__'); |
|---|
| 174 | $entry->text('Elvis was a hero to most but he never meant shit to me'); |
|---|
| 175 | |
|---|
| 176 | is(build($ctx, '<$MTEntryBody encode_xml="1"$>'), "<![CDATA[<p>Elvis was a hero to most but he never meant shit to me</p>]]>", 'MTEntryBody'); |
|---|
| 177 | |
|---|
| 178 | my $comment = MT::Comment->new; |
|---|
| 179 | isa_ok($comment, 'MT::Comment'); |
|---|
| 180 | $comment->entry_id($entry->id); |
|---|
| 181 | $comment->blog_id($entry->blog_id); |
|---|
| 182 | $comment->author('JL'); |
|---|
| 183 | $comment->text('This is not a comment'); |
|---|
| 184 | $comment->visible(1); |
|---|
| 185 | $comment->save or die $comment->errstr; |
|---|
| 186 | |
|---|
| 187 | # Clear caching of things like the comment count of an entry |
|---|
| 188 | $entry = MT::Entry->load($entry->id); |
|---|
| 189 | $ctx->stash('entry', $entry); |
|---|
| 190 | |
|---|
| 191 | is(build($ctx, '<MTComments><$MTCommentBody encode_xml="1"$></MTComments>'), |
|---|
| 192 | "<![CDATA[<p>This is not a comment</p>]]>", 'This is not a comment'); |
|---|
| 193 | is(build($ctx, '<$MTEntryCommentCount$>'), 1, 'MTEntryCommentCount'); |
|---|