root/branches/release-35/t/61-to_from_xml.t @ 1948

Revision 1948, 9.6 kB (checked in by fumiakiy, 20 months ago)

New metadata structure is now backup- and restore-able. BugId:79317

Line 
1#!/usr/bin/perl
2# $Id:$
3use strict;
4use warnings;
5
6use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
7use Test::More qw(no_plan);#tests => 3598;
8
9use MT;
10use MT::Tag;
11use MT::Author;
12use MT::Blog;
13use MT::Role;
14use MT::Category;
15use MT::Entry;
16use MT::TBPing;
17use MT::Comment;
18use MT::Test qw(:db :data);
19
20use vars qw( $DB_DIR $T_CFG );
21
22use MT::BackupRestore;
23use Data::Dumper;
24
25system("rm t/db/* 2>null");
26
27my @emails = ( 'fumiakiy@sixapart.jp', 'fyoshimatsu@sixapart.com' );
28my $chuck = MT::Author->load({ name => 'Chuck D' });
29my $bob = MT::Author->load({ name => 'Bob D' });
30my $mel = MT::Author->load({ name => 'Melody' });
31&setup;
32
33my $mt = MT->new( Config => $T_CFG ) or die MT->errstr;
34isa_ok($mt, 'MT');
35
36my $backup_data = '';
37my $printer = sub { $backup_data .= $_[0] };
38
39my %skip = (
40    'session' => 1,
41    'config'  => 1,
42    'schwartz_job' => 1,
43    'schwartz_error' => 1,
44    'schwartz_funcmap' => 1,
45    'schwartz_exitstatus' => 1,
46);
47my %oldies;
48my $types = MT->registry('object_types');
49foreach my $key (keys %$types) {
50    next if exists $skip{$key};
51    my $iter = MT->model($key)->load_iter;
52    my @data;
53    while ( my $obj = $iter->() ) {
54        push @data, $obj;
55    }
56    $oldies{$key} = \@data;
57}
58
59MT::BackupRestore->backup(undef, $printer, sub {}, sub {}, sub { print $_[0], "\n"; }, 0, 'UTF-8');
60open my $fh, '>', 'c:\\temp\\hoge.txt';
61print $fh $backup_data;
62close $fh;
63
64use IO::String;
65my $h = IO::String->new(\$backup_data);
66my (%objects, %deferred, @errors);
67MT::BackupRestore->restore_process_single_file($h, \%objects, \%deferred, \@errors, "4.0", 0, sub { print $_[0], "\n"; });
68
69is(scalar(keys %deferred), 0);
70warn join "\n", @errors if @errors;
71is(scalar(@errors), 0, 'no error during backup');
72&checkthemout(\%oldies, \%objects);
73
74&finish;
75require MIME::Base64;
76
77sub checkthemout {
78    my ($oldies, $objects) = @_;
79    foreach my $name (keys %$oldies) {
80        my $old_objects = $oldies->{$name};
81        my %meta;
82        for my $old (@$old_objects) {
83            my $class = MT->model($name);
84            isnt($class, undef, "$name must be valid");
85            my $ds = $class->datasource;
86            my $root_class = MT->model($ds);
87            isnt($root_class, undef, "$ds must be valid");
88            my $key = "$root_class#" . $old->id;
89            my $tmp_obj = $objects->{$key};
90            isnt(undef, $tmp_obj, "$key must not hold undef");
91            my $obj = $class->load($tmp_obj->id, {cached_ok=>0});
92            isnt($obj, undef);
93            for my $col (@{$obj->column_names}) {
94                next if $col eq 'id';
95                if ($col =~ /(\w+)_id$/) {
96                    my $parent_name = $1;
97                    my $parent_class = MT->model($parent_name);
98                    next if !defined($parent_class);
99                    if ('ARRAY' ne ref($parent_class)) {
100                        $parent_class = [ $parent_class ];
101                    }
102                    my $old_parent_id = $old->column($col);
103                    next if !defined($old_parent_id); # like MT::Entry's category_id...
104                    next if $old_parent_id eq '0'; # like MT::Trackback's category_id...
105                    my $parent;
106                    foreach (@$parent_class) {
107                        my $parent_key = $_ . '#' . $old_parent_id;
108                        my $new_parent = $objects->{$parent_key};
109                        next unless $new_parent;
110                        $parent = $_->load($new_parent->id);
111                        last if $parent;
112                    }
113                    isnt(undef, $parent);
114                } else {
115                    next if $col eq 'modified_on';
116                    next if (
117                        (defined($old->column($col)) && ($old->column($col) eq '')) &&
118                        (!defined($obj->column($col)))
119                    );
120                    next if ($ds eq 'category' && ($col eq 'parent'));
121                    if ( ($name eq 'trackback') && ($col eq 'is_disabled') ) {
122                        if ( defined($obj->is_disabled) && $obj->is_disabled
123                          && (!defined($obj->entry->allow_pings) || ($obj->entry->allow_pings == 0)) )
124                        {
125                            # is_disabled will be changed upon $entry->save
126                            # and save may occur $comment's post_save trigger
127                            # no harm for the testing purpose, ignore the case.
128                            next;
129                        }
130                    }
131                    if ('HASH' eq ref($old->$col)) {
132                        is(Dumper($old->$col), Dumper($old->$col), $col);
133                    } elsif ('blob' eq $obj->column_defs->{$col}->{type}) {
134                        is(
135                            MIME::Base64::encode_base64($old->$col, ''),
136                            MIME::Base64::encode_base64($obj->$col, ''),
137                            "blob - $col");
138                    } else {
139                        is($old->$col, $obj->$col, "$class<$col>" . $obj->id);
140                    }
141                }
142            }
143            unless ( exists($meta{ref($obj)}) ) {
144                my @metacolumns = MT::Meta->metadata_by_class( ref($obj) );
145                my %metacolumns = map { $_->{name} => $_->{type} } @metacolumns;
146                $meta{ref($obj)} = \%metacolumns
147            }
148            my $metacolumns = $meta{ref($obj)};
149            foreach my $metacol (keys %$metacolumns) {
150                if ( my $type = $metacolumns->{$metacol} ) {
151                    if ( 'vblob' eq $type ) {
152                        if ( defined($old->$metacol) && defined($obj->$metacol) ) {
153                            is(
154                                MIME::Base64::encode_base64($old->$metacol, ''),
155                                MIME::Base64::encode_base64($obj->$metacol, ''),
156                                "vblob - $metacol");
157                        }
158                    }
159                    else {
160                        is($old->$metacol, $obj->$metacol, "$class<meta:$metacol>" . $obj->id);
161                    }
162                }
163            }
164        }
165    }
166}
167
168sub finish {
169    use MT::Notification;
170    MT::Notification->remove({email => $_}) foreach @emails;
171   
172    use MT::TBPing;
173    MT::TBPing->remove({tb_id=>2, blog_id=>1,id=>2});
174   
175    use MT::Role;
176    MT::Role->remove;
177   
178    use MT::Association;
179    MT::Association->remove;
180}
181       
182sub setup {
183    use MT::Notification;
184    my $note = MT::Notification->new;
185    $note->email($emails[0]);
186    $note->blog_id(1);
187    $note->save;
188    $note = undef;
189    my $note2 = MT::Notification->new;
190    $note2->email($emails[1]);
191    $note2->blog_id(1);
192    $note2->save;
193    $note2 = undef;
194
195    my $cat = MT::Category->load({ label => 'bar', blog_id => 1});
196    if ($cat) {
197        $cat->allow_pings(1);
198        $cat->save;
199    }
200   
201    require MT::TBPing;
202    my $ping = MT::TBPing->load(2);
203    if (!$ping) {
204        $ping = new MT::TBPing;
205        $ping->tb_id(2);
206        $ping->blog_id(1);
207        $ping->ip('127.0.0.1');
208        $ping->title('Cat Trackback');
209        $ping->excerpt('Foo Bar Baz Quux');
210        $ping->source_url('http://example.net/');
211        $ping->blog_name("Example Blog 2");
212        $ping->created_on('20050405000000');
213        $ping->id(2);
214        $ping->visible(1);
215        $ping->save
216    }
217
218    my @default_roles = (
219        # { name => 'System Administrator',
220        #   perms => ['administer'] },
221        # { name => 'System Designer',
222        #   perms => ['edit_templates', 'rebuild'] },
223        { name => 'Weblog Administrator',
224          description => 'Can administer the weblog.',
225          perms => ['administer_blog'] },
226        { name => 'Designer',
227          description => 'Can edit, manage and rebuild weblog templates.',
228          perms => ['edit_templates', 'rebuild'] },
229        { name => 'Editor',
230          description => 'Can edit all entries/categories/tags on a weblog and rebuild.',
231          perms => ['edit_all_posts', 'edit_categories', 'rebuild', 'edit_tags'], },
232        { name => 'Editor (can upload)',
233          description => 'Can upload files, edit all entries/categories/tags on a weblog and rebuild.',
234          perms => ['edit_all_posts', 'edit_categories', 'edit_tags', 'rebuild', 'upload'], },
235        { name => 'Publisher',
236          description => 'Can upload files, edit all entries/categories/tags on a weblog, rebuild and send notifications.',
237          perms => ['edit_all_posts', 'edit_categories', 'edit_tags', 'send_notifications', 'rebuild', 'upload'], },
238#        { name => 'Writer',
239#          description => 'Can create entries and edit their own.',
240#          perms => ['post'], },
241#        { name => 'Writer (can upload)',
242#          description => 'Can create entries, edit their own and upload files.',
243#          perms => ['post', 'upload'], },
244        # { name => 'System Blog Administrator',
245        #   perms => ['administer_blog'] },
246    );
247
248    require MT::Role;
249    foreach my $r (@default_roles) {
250        my $role = MT::Role->new();
251        $role->name(MT->translate($r->{name}));
252        $role->description(MT->translate($r->{description}));
253        $role->clear_full_permissions;
254        $role->set_these_permissions($r->{perms});
255        if ($r->{name} =~ m/^System/) {
256            $role->is_system(1);
257        }
258        $role->save;
259    }
260
261    require MT::Association;
262    my $b1 = MT::Blog->load(1);
263    my $r = MT::Role->load({ name => 'Weblog Administrator' });
264    MT::Association->link($chuck => $r => $b1); # Chuck is a weblog admin
265
266    my $r2 = MT::Role->load({ name => 'Publisher' });
267    MT::Association->link($bob => $r2 => $b1); # Bob is a publisher
268
269    my $r3 = MT::Role->load({ name => 'Writer' });
270    MT::Association->link($mel => $r3 => $b1); # Melody is a writer
271}
Note: See TracBrowser for help on using the browser.