root/branches/release-41/t/28-xmlrpc.t @ 2709

Revision 2709, 21.2 kB (checked in by bchoate, 17 months ago)

Fixes for XMLRPC test suite.

  • Property svn:mime-type set to text/plain
  • Property svn:keywords set to Author Date Id Revision
Line 
1use strict;
2use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
3
4BEGIN {
5    $ENV{MT_HOME} = './';
6};
7
8use MT;
9
10use Test::More qw( no_plan );
11use MIME::Base64;
12
13# To keep away from being under FastCGI
14$ENV{HTTP_HOST} = 'localhost';
15
16use MT::Test qw(:db :data);
17my $mt = MT->new() or die MT->errstr;
18isa_ok($mt, 'MT');
19
20my $base_uri = '/mt-xmlrpc.cgi';
21my $username = 'Chuck D';
22my $password = 'seecret';
23
24use XMLRPC::Lite;
25my $ser   = XMLRPC::Serializer->new();
26my $deser = XMLRPC::Deserializer->new();
27
28require LWP::UserAgent::Local;
29my $ua = new LWP::UserAgent::Local({ ScriptAlias => '/' });
30
31my $logo = q{R0lGODlhlgAZANUAAP////Ly8uTk5NfX18nJyb29vbu7u62trZ6eno+Pj1iZvVKQsoCAgE6IqHx8fEmAnnFxcUV4lGtra0BwimFhYTtnf1lZWTZfdVBQUDFWaitMXz8/PydDUyA5SC0tLRswPBYlLxkZGQ8bIggPEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAHAP8ALAAAAACWABkAAAb/QAjBQCoaMQSC0QiaKJ6LzGhJIhyoVQSWQcBgi8lwIkTdEDZfkjCchHzZbC3JfF0eCOQign3wLOFJRgWDFgUkBRYJAAAUVAaLVBxPk08PU0YHAH5GFABuVAIARF8ABAkIA6VUGABeX4oJsbGuVLKZB7GfJI+NRQwACX8CsQQAAZskpbKxS4YOBRIFDooBSkYeiwBLH08cSxEKD0udwUYGAXlGEACpyEvA5ppLrLS12mlf9FghAQJkHgEGUAlUZF25ZAexGILGUBECeXoAZPoWjgqIbksECLgmEcsAARsAyKECL6I7fa/u4VvV6ss6LcXQ/LFGAtvBkl8WHjqkCFsd/34GFBkZ8eQDlgoKKixR5OqXzCMAGJAYgA6LRAwYElRjWW9pK6xY0+Vr+QaYSCwEPTzSdRXs0zRCH5H5lVUltwlfiEZY4rOIRiznyKzTZSTbogBdUWJRZJhsGsVUAALwh7ZxnSKNVa0UyirYX6FFRChYcGmJpAUD0bGSyrdjkYBWD2ClkKoeZK9gw668veRXr4HDElBwl0y225UkQGvs5AZ0kQUKLlAZAV2BUXWeMoklkWlAmACOMR8MIWpeeK/IueLjXYWm1YTpQa8LUNU5iQxPQCzB/wTvkmquGcHPAMsocpl4AmpWBHvJqZQeCQxCeB4Y7pEEH3Kg8XNWg1Q8EP9OBiDyN4k3mCzS1S+EVQERZsbR5ol5DOSWDlO5vYVFhOwRRIpxYBFnjxEPbWJfERdQYiRGRYS0EShLLmHQO4YFAB8rjdHCmGEVqvfYhO3hkxlOX3hASwhPifmFCBpU0MCRT0ixoI0QwhnnPLl9EUJuuhUhJp5yCojBdkvcCegcfUKIJwY+PqioCBdUR8kCF1yn6KSUVmrppZheKoKHbIajQWmZhirqqKReOgKnnTagX6mstupqqRd1CgWor9Zq661fICVrUrj26qutsU4S6QcfdPAErb8mq6ylaz7RABVFSrrstNQi50RR0y0gbbXcdkuCBk/shQUHInhrLrei8XoZ7rrrFmkdu/B664S48dY7LQcPkGjvvksEAQA7};
32
33my @apis = (
34    {
35        api    =>'blogger.getUsersBlogs',
36        params => [ '', $username, $password ],
37        result => sub {
38            my ( $som ) = @_;
39            my $result = $som->result;
40            is( $result->[0]->{url}, 'http://narnia.na/nana/', 'url is correct' );
41            is( $result->[0]->{blogid}, 1, 'blogid is correct' );
42            is( $result->[0]->{blogName}, 'none', 'blogName is correct' );
43        }
44    },
45    {
46        api    => 'blogger.getUserInfo',
47        params => [ '', $username, $password ],
48        result => sub {
49            my ( $som ) = @_;
50            my $result = $som->result;
51            my $author = MT::Author->load({ name => 'Chuck D' });
52            is( $result->{userid}, $author->id);
53            is( $result->{firstname}, (split /\s+/, $author->name)[0]);
54            is( $result->{lastname}, (split /\s+/, $author->name)[1]);
55            is( $result->{nickname}, $author->nickname || '');
56            is( $result->{email}, $author->email || '');
57            is( $result->{url}, $author->url || '');
58        }
59    },
60    {
61        api    => 'blogger.getUsersBlogs',
62        params => [ '', 'Chuck D', 'wrong' ],
63        result => sub {
64            my ( $som ) = @_;
65            ok(!$som->result);
66            ok($som->fault);
67            is($som->faultstring, 'Invalid login');
68            is($som->faultcode, 1);
69        }
70    },
71    {
72        api    => 'blogger.getUserInfo',
73        params => [ '', 'Chuck D', 'wrong' ],
74        result => sub {
75            my ( $som ) = @_;
76            ok(!$som->result);
77            ok($som->fault);
78            is($som->faultstring, 'Invalid login');
79            is($som->faultcode, 1);
80        }
81    },
82    {
83        api    => 'blogger.getRecentPosts',
84        params => [ '', 1, $username, $password, 2 ],
85        result => sub {
86            my ( $som ) = @_;
87            my $result = $som->result;
88            is(scalar(@$result), 2);
89            my $entry  = MT::Entry->load(3);
90            my $author = MT::Author->load({ name => 'Chuck D' });
91            is($result->[0]->{userid}, $author->id);
92            is($result->[0]->{postid}, $entry->id);
93            my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d",
94                unpack 'A4A2A2A2A2A2', $entry->authored_on;
95            is($result->[0]->{dateCreated}, $ao);
96            is($result->[0]->{content}, $entry->text);
97            $entry  = MT::Entry->load(2);
98            $author = MT::Author->load({ name => 'Bob D' });
99            is($result->[1]->{userid}, $author->id);
100            is($result->[1]->{postid}, $entry->id);
101            my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d",
102                unpack 'A4A2A2A2A2A2', $entry->authored_on;
103            is($result->[1]->{dateCreated}, $ao);
104            is($result->[1]->{content}, $entry->text);
105        },
106    },
107    {
108        api    => 'metaWeblog.getRecentPosts',
109        params => [ 1, $username, $password, 2 ],
110        result => sub {
111            my ( $som ) = @_;
112            my $result = $som->result;
113            is(scalar(@$result), 2);
114            my $author = MT::Author->load({ name => 'Chuck D' });
115            my $entry  = MT::Entry->load(3);
116            is($result->[0]->{userid}, $author->id);
117            is($result->[0]->{postid}, $entry->id);
118            my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d",
119                unpack 'A4A2A2A2A2A2', $entry->authored_on;
120            is($result->[0]->{dateCreated}, $ao);
121            is($result->[0]->{description}, $entry->text);
122            is($result->[0]->{title}, $entry->title);
123            is($result->[0]->{link}, $entry->permalink);
124            is($result->[0]->{permaLink}, $entry->permalink);
125            is($result->[0]->{mt_excerpt},
126                defined $entry->excerpt ? $entry->excerpt : '' );
127            is($result->[0]->{mt_text_more}, $entry->text_more);
128            is($result->[0]->{mt_allow_comments}, $entry->allow_comments);
129            is($result->[0]->{mt_allow_pings}, 0);
130            is($result->[0]->{mt_convert_breaks}, $entry->convert_breaks || '');
131            is($result->[0]->{mt_keywords}, '');
132            $author = MT::Author->load({ name => 'Bob D' });
133            $entry  = MT::Entry->load(2);
134            is($result->[1]->{userid}, $author->id);
135            is($result->[1]->{postid}, $entry->id);
136            $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d",
137                unpack 'A4A2A2A2A2A2', $entry->authored_on;
138            is($result->[1]->{dateCreated}, $ao);
139            is($result->[1]->{description}, $entry->text);
140            is($result->[1]->{title}, $entry->title);
141            is($result->[1]->{link}, $entry->permalink);
142            is($result->[1]->{permaLink}, $entry->permalink);
143            is($result->[1]->{mt_excerpt},
144                defined $entry->excerpt ? $entry->excerpt : '' );
145            is($result->[1]->{mt_text_more}, $entry->text_more);
146            is($result->[1]->{mt_allow_comments}, $entry->allow_comments);
147            is($result->[1]->{mt_allow_pings}, $entry->allow_pings || '');
148            is($result->[1]->{mt_convert_breaks}, $entry->convert_breaks || '');
149            is($result->[1]->{mt_keywords}, $entry->keywords || '');
150        },
151    },
152    {
153        api    => 'mt.getRecentPostTitles',
154        params => [ 1, $username, $password, 3 ],
155        result => sub {
156            my ( $som ) = @_;
157            my $result = $som->result;
158            is(scalar(@$result), 3);
159            my @entries = MT::Entry->load({
160                blog_id => 1,
161            }, {
162                'sort' => 'authored_on',
163                'direction' => 'descend',
164                limit => 3,
165            });
166            for ( my $i = 0; $i < 3; ++$i ) {
167                is( $entries[$i]->id, $result->[$i]->{postid} );
168                is( $entries[$i]->title, $result->[$i]->{title} );
169            }
170        },
171    },
172    {
173        api    => 'blogger.editPost',
174        params => [ '', 3, $username, $password, 'Foo Bar', 0 ],
175        result => sub {
176            my ( $som ) = @_;
177            my $result = $som->result;
178            MT::Entry->driver->Disabled(1);
179            my $entry = MT::Entry->load(3);
180            MT::Entry->driver->Disabled(0);
181            is($entry->text, 'Foo Bar');
182        },
183    },
184    {
185        api    => 'metaWeblog.editPost',
186        params => [ 3, $username, $password, {
187            title => 'Title',
188            description => 'Description',
189            mt_convert_breaks => 'wiki',
190            mt_allow_comments => 1,
191            mt_allow_pings => 1,
192            mt_excerpt => 'Excerpt',
193            mt_text_more => 'Extended Entry',
194            mt_keywords => 'Keywords',
195            mt_tb_ping_urls => [ 'http://127.0.0.1/' ],
196            dateCreated => '19770922T15:30:00',
197        }, 0 ],
198        result => sub {
199            my ( $som ) = @_;
200            my $result = $som->result;
201            MT::Entry->driver->Disabled(1);
202            my $entry = MT::Entry->load(3);
203            MT::Entry->driver->Disabled(0);
204            is($entry->title, 'Title');
205            is($entry->text, 'Description');
206            is($entry->convert_breaks, 'wiki');
207            is($entry->allow_comments, 1);
208            is($entry->allow_pings, 1);
209            is($entry->excerpt, 'Excerpt');
210            is($entry->text_more, 'Extended Entry');
211            is($entry->keywords, 'Keywords');
212            is($entry->to_ping_urls, 'http://127.0.0.1/');
213            is($entry->to_ping_url_list->[0], 'http://127.0.0.1/');
214            is($entry->authored_on, '19770922153000');
215        },
216    },
217    {
218        api    => 'metaWeblog.editPost',
219        params => [ 3, $username, $password, {
220            mt_convert_breaks => '',
221            mt_allow_comments => 2,
222            mt_excerpt => '',
223            mt_text_more => '',
224        }, 0 ],
225        result => sub {
226            my ( $som ) = @_;
227            my $result = $som->result;
228        },
229    },
230    {
231        api    => 'mt.getCategoryList',
232        params => [ 1, $username, $password ],
233        result => sub {
234            my ( $som ) = @_;
235            my $result = $som->result;
236            my $cat1 = MT::Category->load(1);
237            my $cat2 = MT::Category->load(2);
238            is($result->[0]->{categoryId}, $cat1->id);
239            is($result->[0]->{categoryName}, $cat1->label);
240            is($result->[1]->{categoryId}, $cat2->id);
241            is($result->[1]->{categoryName}, $cat2->label);
242        },
243    },
244    {
245        api    => 'mt.getPostCategories',
246        params => [ 3, $username, $password ],
247        result => sub {
248            my ( $som ) = @_;
249            my $result = $som->result;
250            is(scalar @{ $som->result }, 0);
251        },
252    },
253    {
254        api    => 'mt.setPostCategories',
255        params => [ 3, $username, $password, [
256            { categoryId => 1 } ]
257        ],
258        result => sub {
259            my ( $som ) = @_;
260            my $result = $som->result;
261            MT::Entry->driver->Disabled(1);
262            my $entry = MT::Entry->load(3);
263            MT::Entry->driver->Disabled(0);
264            my $cat1 = MT::Category->load(1);
265            my $cats = $entry->categories;
266            is(scalar @$cats, 1);
267            is($cats->[0]->label, $cat1->label);
268            is($entry->category->label, $cat1->label);
269        },
270    },
271    {
272        api    => 'mt.setPostCategories',
273        params => [ 3, $username, $password, [
274            { categoryId => 1, isPrimary => 1 },
275            { categoryId => 2, isPrimary => 0 },
276        ] ],
277        pre    => sub {
278            my $r = MT->request;
279            my $oc = $r->cache('object_cache', {});
280        },
281        result => sub {
282            my ( $som ) = @_;
283            my $result = $som->result;
284            MT::Entry->driver->Disabled(1);
285            my $entry = MT::Entry->load(3);
286            MT::Entry->driver->Disabled(0);
287            my $cat1 = MT::Category->load(1);
288            my $cats = $entry->categories;
289            is(scalar @$cats, 2);
290            is($entry->category->label, $cat1->label);
291        },
292    },
293    {
294        api    => 'mt.setPostCategories',
295        params => [ 3, $username, $password, [
296            { categoryId => 1, isPrimary => 0 },
297            { categoryId => 2, isPrimary => 1 },
298        ] ],
299        pre    => sub {
300            my $r = MT->request;
301            my $oc = $r->cache('object_cache', {});
302        },
303        result => sub {
304            my ( $som ) = @_;
305            my $result = $som->result;
306            MT::Entry->driver->Disabled(1);
307            my $entry = MT::Entry->load(3);
308            MT::Entry->driver->Disabled(0);
309            my $cat2 = MT::Category->load(2);
310            my $cats = $entry->categories;
311            is(scalar @$cats, 2);
312            is($entry->category->label, $cat2->label);
313        },
314    },
315    {
316        api    => 'mt.setPostCategories',
317        params => [ 3, $username, $password, [
318        ] ],
319        pre    => sub {
320            my $r = MT->request;
321            my $oc = $r->cache('object_cache', {});
322        },
323        result => sub {
324            my ( $som ) = @_;
325            my $result = $som->result;
326            MT::Entry->driver->Disabled(1);
327            my $entry = MT::Entry->load(3);
328            MT::Entry->driver->Disabled(0);
329            my $cats = $entry->categories;
330            is(scalar @$cats, 0);
331            ok(!$entry->category);
332        },
333    },
334    {
335        api    => 'blogger.newPost',
336        params => [ '', 1, $username, $password, 'This is a new post.', 0 ],
337        result => sub {
338            my ( $som ) = @_;
339            my $result = $som->result;
340            my $entry = MT::Entry->load($result);
341            ok($entry);
342            is($entry->text, 'This is a new post.');
343            # RELEASE unless NoPublishMeansDraft
344            is($entry->status, MT::Entry::RELEASE());
345        },
346    },
347    {
348        api    => 'metaWeblog.newPost',
349        params => [ 1, $username, $password, {
350            title => 'MetaWeblog Post',
351            description => 'This is a new post via metaWeblog API.'
352        }, 1 ],
353        result => sub {
354            my ( $som ) = @_;
355            my $result = $som->result;
356            my $entry = MT::Entry->load($result);
357            ok($entry);
358            is($entry->title, 'MetaWeblog Post');
359            is($entry->text, 'This is a new post via metaWeblog API.');
360            # RELEASE unless NoPublishMeansDraft
361            is($entry->status, MT::Entry::RELEASE());
362        },
363    },
364    {
365        api    => 'blogger.deletePost',
366        params => [ '', 25, $username, $password, 0 ],
367        pre    => sub {
368            my ( $e1 ) = MT::Entry->load(undef,
369                { sort => 'created_on', direction => 'descend', limit => 1 }
370            );
371            is( $e1->id, 25 );
372            return $e1;
373        },
374        result => sub {
375            my ( $som, $data ) = @_;
376            my $result = $som->result;
377            MT::Entry->driver->Disabled(1);
378            my $entry = MT::Entry->load($data->id);
379            is( $entry, undef );
380            MT::Entry->driver->Disabled(0);
381        },
382    },
383    {
384        api    => 'metaWeblog.getPost',
385        params => [ 24, $username, $password ],
386        pre    => sub {
387            my ( $e2 ) = MT::Entry->load(undef,
388                { sort => 'created_on', direction => 'descend', limit => 1 }
389            );
390            is( $e2->id, 24 );
391            return $e2;
392        },
393        result => sub {
394            my ( $som ) = @_;
395            my $result = $som->result;
396            my $entry = MT::Entry->load(24);
397            ok($entry);
398            is($entry->permalink, $result->{permaLink});
399            is($entry->basename, $result->{mt_basename});
400            is($entry->text, $result->{description});
401        },
402    },
403    {
404        api    => 'metaWeblog.deletePost',
405        params => [ '', 24, $username, $password, 0 ],
406        pre    => sub {
407            my ( $e2 ) = MT::Entry->load(undef,
408                { sort => 'created_on', direction => 'descend', limit => 1 }
409            );
410            is( $e2->id, 24 );
411            return $e2;
412        },
413        result => sub {
414            my ( $som, $data ) = @_;
415            my $result = $som->result;
416            MT::Entry->driver->Disabled(1);
417            my $entry = MT::Entry->load($data->id);
418            is( $entry, undef );
419            MT::Entry->driver->Disabled(0);
420        },
421    },
422    {
423        api    => 'metaWeblog.newMediaObject',
424        params => [ 1, $username, $password, {
425            name => 'movable-type-logo.gif',
426            type => 'image/gif',
427            bits  => sub {
428                return MIME::Base64::decode_base64($logo);
429            },
430        } ],
431        result => sub {
432            my ( $som ) = @_;
433            my $result = $som->result;
434            my $url = $result->{url};
435            is( $url, 'http://narnia.na/nana/movable-type-logo.gif' );
436            my $asset = MT::Asset::Image->load(undef, { sort => 'created_on', direction => 'descend', limit => 1 });
437            ok($asset, 'asset loaded');
438            is( $asset->mime_type, 'image/gif' );
439            is( $asset->file_name, 'movable-type-logo.gif' );
440            local $/;
441            open my $fh, '<', $asset->file_path;
442            my $image = <$fh>;
443            close $fh;
444            require MIME::Base64;
445            is( $logo, MIME::Base64::encode_base64($image, '') );
446        },
447        post   => sub {
448            my $asset = MT::Asset->load(undef, { sort => 'created_on', direction => 'descend', limit => 1 });
449            $asset->remove();
450        }
451    },
452    {
453        api    => 'metaWeblog.getCategories',
454        params => [ 1, $username, $password ],
455        result => sub {
456            my ( $som ) = @_;
457            my $result = $som->result;
458            my @cats = MT::Category->load({blog_id=>1});
459            for ( my $i = 0; $i <= $#cats; ++$i ) {
460                is($cats[$i]->id, $result->[$i]->{categoryId});
461                is($cats[$i]->label, $result->[$i]->{categoryName});
462                is($cats[$i]->description, $result->[$i]->{description});
463                if ( my $parent = $cats[$i]->parent_category ) {
464                    is($parent->id, $result->[$i]->{parentId});
465                }
466                else {
467                    ok(!($result->[$i]->{parentId}));
468                }
469            }
470            is(scalar(@$result), scalar(@cats));
471        },
472    },
473    {
474        api    => 'mt.getTrackbackPings',
475        params => [ 1, $username, $password ],
476        result => sub {
477            my ( $som ) = @_;
478            my $result = $som->result;
479            my @pings = MT::TBPing->load( undef, {
480                'join' => MT::Trackback->join_on( undef, {
481                    id => \'= tbping_tb_id',
482                }, {
483                    'join' => MT::Entry->join_on( undef, {
484                        id => \'= trackback_entry_id',
485                        class => 'entry',
486                    }
487                ) }
488            ) } );
489            for ( my $i = 0; $i <= $#pings; ++$i ) {
490                is($pings[$i]->ip, $result->[$i]->{pingIP});
491                is($pings[$i]->source_url, $result->[$i]->{pingURL});
492                is($pings[$i]->title, $result->[$i]->{pingTitle});
493            }
494            is(scalar(@$result), scalar(@pings));
495        },
496    },
497    {
498        api    => 'mt.supportedTextFilters',
499        params => [ $username, $password ],
500        result => sub {
501            my ( $som ) = @_;
502            my $result = $som->result;
503            my %tf = (
504                '__default__' => 1,
505                'richtext' => 1,
506                'markdown' => 1,
507                'markdown_with_smartypants' => 1,
508                'textile_2' => 1,
509            );
510            foreach my $res ( @$result ) {
511                is(1, delete( $tf{$res->{key}} ), $res->{key});
512            }
513            ok(!%tf);
514        },
515    },
516    {
517        api    => 'mt.getTagList',
518        params => [ 1, $username, $password ],
519        result => sub {
520            my ( $som ) = @_;
521            my $result = $som->result;
522            my @tags = MT::Tag->load(undef, {
523                'join' => MT::ObjectTag->join_on(undef, {
524                    tag_id => \'= tag_id',
525                    blog_id => 1
526                }, { unique => 1 })
527            });
528            for ( my $i = 0; $i <= $#tags; ++$i ) {
529                is($tags[$i]->id, $result->[$i]->{tagId});
530                is($tags[$i]->name, $result->[$i]->{tagName});
531            }
532            is(scalar(@$result), scalar(@tags));
533        },
534    },
535    #TODO Add these tests
536    #'wp.newPage', 'wp.getPages', 'wp.getPage', 'wp.editPage', 'wp.deletePage',
537    # 'mt.publishPost',
538    # newPost with mt_tags
539);
540
541my $uri = new URI();
542$uri->path($base_uri);
543my $req = new HTTP::Request(POST => $uri);
544
545foreach my $api ( @apis ) {
546
547    my $data = {};
548    $data = $api->{pre}->() if exists $api->{pre};
549    my @params;
550    foreach my $param ( @{ $api->{params} } ) {
551        if ( 'CODE' eq ref($param) ) {
552            push @params, $param->();
553        }
554        elsif ( 'HASH' eq ref($param) ) {
555            my $hash = {};
556            while ( my ( $key, $val ) = each %$param ) {
557                if ( 'CODE' eq ref($val) ) {
558                    $hash->{$key} = $val->();
559                }
560                else {
561                    $hash->{$key} = $val;
562                }
563            }
564            push @params, $hash;
565        }
566        else {
567            push @params, $param;
568        }
569    }
570    $req->content($ser->method($api->{api}, @params));
571
572    my $resp = $ua->request($req);
573#    print STDERR $resp->content;
574    my $som = $deser->deserialize($resp->content());
575    $api->{result}->($som, $data);
576    $api->{post}->() if exists $api->{post};
577}
578
5791;
580__END__
Note: See TracBrowser for help on using the browser.