| 1 | use strict; |
|---|
| 2 | use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib'; |
|---|
| 3 | |
|---|
| 4 | BEGIN { |
|---|
| 5 | $ENV{MT_HOME} = './'; |
|---|
| 6 | }; |
|---|
| 7 | |
|---|
| 8 | use MT; |
|---|
| 9 | |
|---|
| 10 | use Test::More qw( no_plan ); |
|---|
| 11 | |
|---|
| 12 | # To keep away from being under FastCGI |
|---|
| 13 | $ENV{HTTP_HOST} = 'localhost'; |
|---|
| 14 | |
|---|
| 15 | use vars qw( $DB_DIR $T_CFG ); |
|---|
| 16 | my $mt = MT->new( Config => $T_CFG ) or die MT->errstr; |
|---|
| 17 | isa_ok($mt, 'MT'); |
|---|
| 18 | |
|---|
| 19 | use MT::Test qw(:db :data); |
|---|
| 20 | |
|---|
| 21 | my $base_uri = '/mt-xmlrpc.cgi'; |
|---|
| 22 | my $username = 'Chuck D'; |
|---|
| 23 | my $password = 'seecret'; |
|---|
| 24 | |
|---|
| 25 | use XMLRPC::Lite; |
|---|
| 26 | my $ser = XMLRPC::Serializer->new(); |
|---|
| 27 | my $deser = XMLRPC::Deserializer->new(); |
|---|
| 28 | |
|---|
| 29 | require LWP::UserAgent::Local; |
|---|
| 30 | my $ua = new LWP::UserAgent::Local({ ScriptAlias => '/' }); |
|---|
| 31 | |
|---|
| 32 | my @apis = ( |
|---|
| 33 | { |
|---|
| 34 | api =>'blogger.getUsersBlogs', |
|---|
| 35 | params => [ '', $username, $password ], |
|---|
| 36 | result => sub { |
|---|
| 37 | my ( $som ) = @_; |
|---|
| 38 | my $result = $som->result; |
|---|
| 39 | is( $result->[0]->{url}, 'http://narnia.na/nana/', 'url is correct' ); |
|---|
| 40 | is( $result->[0]->{blogid}, 1, 'blogid is correct' ); |
|---|
| 41 | is( $result->[0]->{blogName}, 'none', 'blogName is correct' ); |
|---|
| 42 | } |
|---|
| 43 | }, |
|---|
| 44 | { |
|---|
| 45 | api => 'blogger.getUserInfo', |
|---|
| 46 | params => [ '', $username, $password ], |
|---|
| 47 | result => sub { |
|---|
| 48 | my ( $som ) = @_; |
|---|
| 49 | my $result = $som->result; |
|---|
| 50 | my $author = MT::Author->load({ name => 'Chuck D' }); |
|---|
| 51 | is( $result->{userid}, $author->id); |
|---|
| 52 | is( $result->{firstname}, (split /\s+/, $author->name)[0]); |
|---|
| 53 | is( $result->{lastname}, (split /\s+/, $author->name)[1]); |
|---|
| 54 | is( $result->{nickname}, $author->nickname || ''); |
|---|
| 55 | is( $result->{email}, $author->email || ''); |
|---|
| 56 | is( $result->{url}, $author->url || ''); |
|---|
| 57 | } |
|---|
| 58 | }, |
|---|
| 59 | { |
|---|
| 60 | api => 'blogger.getUsersBlogs', |
|---|
| 61 | params => [ '', 'Chuck D', 'wrong' ], |
|---|
| 62 | result => sub { |
|---|
| 63 | my ( $som ) = @_; |
|---|
| 64 | ok(!$som->result); |
|---|
| 65 | ok($som->fault); |
|---|
| 66 | is($som->faultstring, 'Invalid login'); |
|---|
| 67 | is($som->faultcode, 1); |
|---|
| 68 | } |
|---|
| 69 | }, |
|---|
| 70 | { |
|---|
| 71 | api => 'blogger.getUserInfo', |
|---|
| 72 | params => [ '', 'Chuck D', 'wrong' ], |
|---|
| 73 | result => sub { |
|---|
| 74 | my ( $som ) = @_; |
|---|
| 75 | ok(!$som->result); |
|---|
| 76 | ok($som->fault); |
|---|
| 77 | is($som->faultstring, 'Invalid login'); |
|---|
| 78 | is($som->faultcode, 1); |
|---|
| 79 | } |
|---|
| 80 | }, |
|---|
| 81 | { |
|---|
| 82 | api => 'blogger.getRecentPosts', |
|---|
| 83 | params => [ '', 1, $username, $password, 2 ], |
|---|
| 84 | result => sub { |
|---|
| 85 | my ( $som ) = @_; |
|---|
| 86 | my $result = $som->result; |
|---|
| 87 | is(scalar(@$result), 2); |
|---|
| 88 | my $entry = MT::Entry->load(3); |
|---|
| 89 | my $author = MT::Author->load({ name => 'Chuck D' }); |
|---|
| 90 | is($result->[0]->{userid}, $author->id); |
|---|
| 91 | is($result->[0]->{postid}, $entry->id); |
|---|
| 92 | my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d", |
|---|
| 93 | unpack 'A4A2A2A2A2A2', $entry->authored_on; |
|---|
| 94 | is($result->[0]->{dateCreated}, $ao); |
|---|
| 95 | is($result->[0]->{content}, $entry->text); |
|---|
| 96 | $entry = MT::Entry->load(2); |
|---|
| 97 | $author = MT::Author->load({ name => 'Bob D' }); |
|---|
| 98 | is($result->[1]->{userid}, $author->id); |
|---|
| 99 | is($result->[1]->{postid}, $entry->id); |
|---|
| 100 | my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d", |
|---|
| 101 | unpack 'A4A2A2A2A2A2', $entry->authored_on; |
|---|
| 102 | is($result->[1]->{dateCreated}, $ao); |
|---|
| 103 | is($result->[1]->{content}, $entry->text); |
|---|
| 104 | }, |
|---|
| 105 | }, |
|---|
| 106 | { |
|---|
| 107 | api => 'metaWeblog.getRecentPosts', |
|---|
| 108 | params => [ 1, $username, $password, 2 ], |
|---|
| 109 | result => sub { |
|---|
| 110 | my ( $som ) = @_; |
|---|
| 111 | my $result = $som->result; |
|---|
| 112 | is(scalar(@$result), 2); |
|---|
| 113 | my $author = MT::Author->load({ name => 'Chuck D' }); |
|---|
| 114 | my $entry = MT::Entry->load(3); |
|---|
| 115 | is($result->[0]->{userid}, $author->id); |
|---|
| 116 | is($result->[0]->{postid}, $entry->id); |
|---|
| 117 | my $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d", |
|---|
| 118 | unpack 'A4A2A2A2A2A2', $entry->authored_on; |
|---|
| 119 | is($result->[0]->{dateCreated}, $ao); |
|---|
| 120 | is($result->[0]->{description}, $entry->text); |
|---|
| 121 | is($result->[0]->{title}, $entry->title); |
|---|
| 122 | is($result->[0]->{link}, $entry->permalink); |
|---|
| 123 | is($result->[0]->{permaLink}, $entry->permalink); |
|---|
| 124 | is($result->[0]->{mt_excerpt}, |
|---|
| 125 | defined $entry->excerpt ? $entry->excerpt : '' ); |
|---|
| 126 | is($result->[0]->{mt_text_more}, $entry->text_more); |
|---|
| 127 | is($result->[0]->{mt_allow_comments}, $entry->allow_comments); |
|---|
| 128 | is($result->[0]->{mt_allow_pings}, 0); |
|---|
| 129 | is($result->[0]->{mt_convert_breaks}, $entry->convert_breaks || ''); |
|---|
| 130 | is($result->[0]->{mt_keywords}, ''); |
|---|
| 131 | $author = MT::Author->load({ name => 'Bob D' }); |
|---|
| 132 | $entry = MT::Entry->load(2); |
|---|
| 133 | is($result->[1]->{userid}, $author->id); |
|---|
| 134 | is($result->[1]->{postid}, $entry->id); |
|---|
| 135 | $ao = sprintf "%04d%02d%02dT%02d:%02d:%02d", |
|---|
| 136 | unpack 'A4A2A2A2A2A2', $entry->authored_on; |
|---|
| 137 | is($result->[1]->{dateCreated}, $ao); |
|---|
| 138 | is($result->[1]->{description}, $entry->text); |
|---|
| 139 | is($result->[1]->{title}, $entry->title); |
|---|
| 140 | is($result->[1]->{link}, $entry->permalink); |
|---|
| 141 | is($result->[1]->{permaLink}, $entry->permalink); |
|---|
| 142 | is($result->[1]->{mt_excerpt}, |
|---|
| 143 | defined $entry->excerpt ? $entry->excerpt : '' ); |
|---|
| 144 | is($result->[1]->{mt_text_more}, $entry->text_more); |
|---|
| 145 | is($result->[1]->{mt_allow_comments}, $entry->allow_comments); |
|---|
| 146 | is($result->[1]->{mt_allow_pings}, $entry->allow_pings || ''); |
|---|
| 147 | is($result->[1]->{mt_convert_breaks}, $entry->convert_breaks || ''); |
|---|
| 148 | is($result->[1]->{mt_keywords}, $entry->keywords || ''); |
|---|
| 149 | }, |
|---|
| 150 | }, |
|---|
| 151 | { |
|---|
| 152 | api => 'blogger.editPost', |
|---|
| 153 | params => [ '', 3, $username, $password, 'Foo Bar', 0 ], |
|---|
| 154 | result => sub { |
|---|
| 155 | my ( $som ) = @_; |
|---|
| 156 | my $result = $som->result; |
|---|
| 157 | MT::Entry->driver->Disabled(1); |
|---|
| 158 | my $entry = MT::Entry->load(3); |
|---|
| 159 | MT::Entry->driver->Disabled(0); |
|---|
| 160 | is($entry->text, 'Foo Bar'); |
|---|
| 161 | }, |
|---|
| 162 | }, |
|---|
| 163 | { |
|---|
| 164 | api => 'metaWeblog.editPost', |
|---|
| 165 | params => [ 3, $username, $password, { |
|---|
| 166 | title => 'Title', |
|---|
| 167 | description => 'Description', |
|---|
| 168 | mt_convert_breaks => 'wiki', |
|---|
| 169 | mt_allow_comments => 1, |
|---|
| 170 | mt_allow_pings => 1, |
|---|
| 171 | mt_excerpt => 'Excerpt', |
|---|
| 172 | mt_text_more => 'Extended Entry', |
|---|
| 173 | mt_keywords => 'Keywords', |
|---|
| 174 | mt_tb_ping_urls => [ 'http://127.0.0.1/' ], |
|---|
| 175 | dateCreated => '19770922T15:30:00', |
|---|
| 176 | }, 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->title, 'Title'); |
|---|
| 184 | is($entry->text, 'Description'); |
|---|
| 185 | is($entry->convert_breaks, 'wiki'); |
|---|
| 186 | is($entry->allow_comments, 1); |
|---|
| 187 | is($entry->allow_pings, 1); |
|---|
| 188 | is($entry->excerpt, 'Excerpt'); |
|---|
| 189 | is($entry->text_more, 'Extended Entry'); |
|---|
| 190 | is($entry->keywords, 'Keywords'); |
|---|
| 191 | is($entry->to_ping_urls, 'http://127.0.0.1/'); |
|---|
| 192 | is($entry->to_ping_url_list->[0], 'http://127.0.0.1/'); |
|---|
| 193 | is($entry->authored_on, '19770922153000'); |
|---|
| 194 | }, |
|---|
| 195 | }, |
|---|
| 196 | { |
|---|
| 197 | api => 'metaWeblog.editPost', |
|---|
| 198 | params => [ 3, $username, $password, { |
|---|
| 199 | mt_convert_breaks => '', |
|---|
| 200 | mt_allow_comments => 2, |
|---|
| 201 | mt_excerpt => '', |
|---|
| 202 | mt_text_more => '', |
|---|
| 203 | }, 0 ], |
|---|
| 204 | result => sub { |
|---|
| 205 | my ( $som ) = @_; |
|---|
| 206 | my $result = $som->result; |
|---|
| 207 | }, |
|---|
| 208 | }, |
|---|
| 209 | { |
|---|
| 210 | api => 'mt.getCategoryList', |
|---|
| 211 | params => [ 1, $username, $password ], |
|---|
| 212 | result => sub { |
|---|
| 213 | my ( $som ) = @_; |
|---|
| 214 | my $result = $som->result; |
|---|
| 215 | my $cat1 = MT::Category->load(1); |
|---|
| 216 | my $cat2 = MT::Category->load(2); |
|---|
| 217 | is($result->[0]->{categoryId}, $cat1->id); |
|---|
| 218 | is($result->[0]->{categoryName}, $cat1->label); |
|---|
| 219 | is($result->[1]->{categoryId}, $cat2->id); |
|---|
| 220 | is($result->[1]->{categoryName}, $cat2->label); |
|---|
| 221 | }, |
|---|
| 222 | }, |
|---|
| 223 | { |
|---|
| 224 | api => 'mt.getPostCategories', |
|---|
| 225 | params => [ 3, $username, $password ], |
|---|
| 226 | result => sub { |
|---|
| 227 | my ( $som ) = @_; |
|---|
| 228 | my $result = $som->result; |
|---|
| 229 | is(scalar @{ $som->result }, 0); |
|---|
| 230 | }, |
|---|
| 231 | }, |
|---|
| 232 | { |
|---|
| 233 | api => 'mt.setPostCategories', |
|---|
| 234 | params => [ 3, $username, $password, [ |
|---|
| 235 | { categoryId => 1 } ] |
|---|
| 236 | ], |
|---|
| 237 | result => sub { |
|---|
| 238 | my ( $som ) = @_; |
|---|
| 239 | my $result = $som->result; |
|---|
| 240 | MT::Entry->driver->Disabled(1); |
|---|
| 241 | my $entry = MT::Entry->load(3); |
|---|
| 242 | MT::Entry->driver->Disabled(0); |
|---|
| 243 | my $cat1 = MT::Category->load(1); |
|---|
| 244 | my $cats = $entry->categories; |
|---|
| 245 | is(scalar @$cats, 1); |
|---|
| 246 | is($cats->[0]->label, $cat1->label); |
|---|
| 247 | is($entry->category->label, $cat1->label); |
|---|
| 248 | }, |
|---|
| 249 | }, |
|---|
| 250 | { |
|---|
| 251 | api => 'mt.setPostCategories', |
|---|
| 252 | params => [ 3, $username, $password, [ |
|---|
| 253 | { categoryId => 1, isPrimary => 1 }, |
|---|
| 254 | { categoryId => 2, isPrimary => 0 }, |
|---|
| 255 | ] ], |
|---|
| 256 | pre => sub { |
|---|
| 257 | my $r = MT->request; |
|---|
| 258 | my $oc = $r->cache('object_cache', {}); |
|---|
| 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, 2); |
|---|
| 269 | is($entry->category->label, $cat1->label); |
|---|
| 270 | }, |
|---|
| 271 | }, |
|---|
| 272 | { |
|---|
| 273 | api => 'mt.setPostCategories', |
|---|
| 274 | params => [ 3, $username, $password, [ |
|---|
| 275 | { categoryId => 1, isPrimary => 0 }, |
|---|
| 276 | { categoryId => 2, isPrimary => 1 }, |
|---|
| 277 | ] ], |
|---|
| 278 | pre => sub { |
|---|
| 279 | my $r = MT->request; |
|---|
| 280 | my $oc = $r->cache('object_cache', {}); |
|---|
| 281 | }, |
|---|
| 282 | result => sub { |
|---|
| 283 | my ( $som ) = @_; |
|---|
| 284 | my $result = $som->result; |
|---|
| 285 | MT::Entry->driver->Disabled(1); |
|---|
| 286 | my $entry = MT::Entry->load(3); |
|---|
| 287 | MT::Entry->driver->Disabled(0); |
|---|
| 288 | my $cat2 = MT::Category->load(2); |
|---|
| 289 | my $cats = $entry->categories; |
|---|
| 290 | is(scalar @$cats, 2); |
|---|
| 291 | is($entry->category->label, $cat2->label); |
|---|
| 292 | }, |
|---|
| 293 | }, |
|---|
| 294 | { |
|---|
| 295 | api => 'mt.setPostCategories', |
|---|
| 296 | params => [ 3, $username, $password, [ |
|---|
| 297 | ] ], |
|---|
| 298 | pre => sub { |
|---|
| 299 | my $r = MT->request; |
|---|
| 300 | my $oc = $r->cache('object_cache', {}); |
|---|
| 301 | }, |
|---|
| 302 | result => sub { |
|---|
| 303 | my ( $som ) = @_; |
|---|
| 304 | my $result = $som->result; |
|---|
| 305 | MT::Entry->driver->Disabled(1); |
|---|
| 306 | my $entry = MT::Entry->load(3); |
|---|
| 307 | MT::Entry->driver->Disabled(0); |
|---|
| 308 | my $cats = $entry->categories; |
|---|
| 309 | is(scalar @$cats, 0); |
|---|
| 310 | ok(!$entry->category); |
|---|
| 311 | }, |
|---|
| 312 | }, |
|---|
| 313 | #TODO Add more XML-RPC API |
|---|
| 314 | ); |
|---|
| 315 | |
|---|
| 316 | my $uri = new URI(); |
|---|
| 317 | $uri->path($base_uri); |
|---|
| 318 | my $req = new HTTP::Request(POST => $uri); |
|---|
| 319 | |
|---|
| 320 | foreach my $api ( @apis ) { |
|---|
| 321 | |
|---|
| 322 | my $data = {}; |
|---|
| 323 | $data = $api->{pre}->() if exists $api->{pre}; |
|---|
| 324 | |
|---|
| 325 | $req->content($ser->method($api->{api}, @{ $api->{params} })); |
|---|
| 326 | |
|---|
| 327 | my $resp = $ua->request($req); |
|---|
| 328 | # print STDERR $resp->content; |
|---|
| 329 | my $som = $deser->deserialize($resp->content()); |
|---|
| 330 | $api->{result}->($som, $data); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | 1; |
|---|
| 334 | |
|---|
| 335 | __END__ |
|---|