root/branches/release-39/t/28-xmlrpc.t @ 2529

Revision 2529, 21.3 kB (checked in by fumiakiy, 18 months ago)

Added more tests to xmlrpc.

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