3 use BSE::Test qw(base_url);
9 eval "require Text::CSV;"
10 or plan skip_all => "Text::CSV not available";
15 unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules");
19 use BSE::API qw(bse_init bse_make_article);
21 my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin");
22 ok(bse_init($base_cgi), "initialize api")
23 or print "# failed to bse_init in $base_cgi\n";
27 my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG);
28 [import profile simple$when]
33 [import profile simpleupdate$when]
45 [import profile completefile$when]
49 map_file1_displayName=4
51 map_file1_description=6
54 map_file1_requireUser=9
56 map_file1_hide_from_list=11
64 [import profile updatefile$when]
67 map_file1_description=3
75 [import profile updatefileb$when]
86 [import profile newdup$when]
93 [import profile alias$when]
108 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simple$when", callback => sub { note @_ });
109 $imp->process("t/data/importer/article-simple.csv");
110 @added = sort { $a->title cmp $b->title } $imp->leaves;
112 is(@added, 2, "imported two articles");
113 is($added[0]->title, "test1", "check title of first import");
114 is($added[1]->title, "test2", "check title of second import");
117 $_->remove($cfg) for @added;
122 my $testa = bse_make_article(cfg => $cfg, title => "test updates",
123 linkAlias => "alias$when");
126 my $fh = File::Temp->new;
127 my $filename = $fh->filename;
129 linkAlias\tbody\tfile1_file\timage1_file
130 "alias$when"\t"This is the body text with multiple lines
132 Yes, multiple lines with CSV!"\ttestdata.txt\tt101.jpg
135 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simpleupdate$when", callback => sub { note @_ });
136 $imp->process($filename);
137 my $testb = BSE::TB::Articles->getByPkey($testa->id);
138 like($testb->body, qr/This is the body/, "check the body is updated");
139 my @images = $testb->images;
140 is(@images, 1, "have an image");
141 like($images[0]->image, qr/t101\.jpg/, "check file name");
142 is(-s $images[0]->full_filename, -s "t/data/t101.jpg",
143 "check size matches source");
145 cmp_ok($images[0]->image, '!~', 'data', "check we don't pass full path as basename");
147 my @files = $testb->files;
148 is(@files, 1, "should be 1 file");
149 is($files[0]->displayName, "testdata.txt", "check display name");
150 is(-s $files[0]->full_filename, -s "t/data/testdata.txt", "check size");
155 my $fh = File::Temp->new;
156 my $filename = $fh->filename;
158 "alias$when",testdata.txt,test,"A Test File.txt",local,"A test file from BSE",1,1,1,"Some Notes",1
161 my $imp = BSE::Importer->new(cfg => $cfg, profile => "completefile$when", callback => sub { note @_ });
162 $imp->process($filename);
163 my $testb = BSE::TB::Articles->getByPkey($testa->id);
165 my ($file) = grep $_->name eq "test", $testb->files;
166 ok($file, "found the file with name 'test'")
167 or skip "File not found", 9;
168 is(-s $file->full_filename, -s "t/data/testdata.txt", "check size");
169 is($file->displayName, "A Test File.txt", "displayName");
170 is($file->storage, "local", "storage");
171 is($file->description, "A test file from BSE", "description");
172 is($file->forSale, 1, "forSale");
173 is($file->download, 1, "download");
174 is($file->requireUser, 1, "requireUser");
175 is($file->notes, "Some Notes", "notes");
176 is($file->hide_from_list, 1, "hide_from_list");
181 my $fh = File::Temp->new;
182 my $filename = $fh->filename;
184 "alias$when",test,"New description"
187 my $imp = BSE::Importer->new(cfg => $cfg, profile => "updatefile$when", callback => sub { note @_ });
188 $imp->process($filename);
189 my $testb = BSE::TB::Articles->getByPkey($testa->id);
191 my ($file) = grep $_->name eq "test", $testb->files;
192 ok($file, "found the updated file with name 'test'")
193 or skip "File not found", 9;
194 is($file->description, "New description", "description");
195 # other fields should be unchanged
196 is(-s $file->full_filename, -s "t/data/testdata.txt", "check size");
197 is($file->displayName, "A Test File.txt", "displayName");
198 is($file->storage, "local", "storage");
199 is($file->forSale, 1, "forSale");
200 is($file->download, 1, "download");
201 is($file->requireUser, 1, "requireUser");
202 is($file->notes, "Some Notes", "notes");
203 is($file->hide_from_list, 1, "hide_from_list");
208 my $fh = File::Temp->new;
209 my $filename = $fh->filename;
211 "alias$when",test,t101.jpg
214 my $imp = BSE::Importer->new(cfg => $cfg, profile => "updatefileb$when", callback => sub { note @_ });
215 $imp->process($filename);
216 my $testb = BSE::TB::Articles->getByPkey($testa->id);
218 my ($file) = grep $_->name eq "test", $testb->files;
219 ok($file, "found the updated file with name 'test'")
220 or skip "File not found", 9;
221 is(-s $file->full_filename, -s "t/data/t101.jpg", "check size");
222 is($file->displayName, "t101.jpg", "new displayName");
223 # other fields should be unchanged
224 is($file->storage, "local", "storage");
225 is($file->description, "New description", "description");
226 is($file->forSale, 1, "forSale");
227 is($file->download, 1, "download");
228 is($file->requireUser, 1, "requireUser");
229 is($file->notes, "Some Notes", "notes");
230 is($file->hide_from_list, 1, "hide_from_list");
233 { # fail to duplicate a link alias
234 my $fh = File::Temp->new;
235 my $filename = $fh->filename;
238 "alias$when",test,t101.jpg
241 my $imp = BSE::Importer->new(cfg => $cfg, profile => "newdup$when", callback => sub { note @_ });
242 $imp->process($filename);
243 is_deeply([ $imp->leaves ], [], "should be no updated articles");
246 { # don't strip - and _ from linkAlias
247 my $fh = File::Temp->new;
248 my $filename = $fh->filename;
251 $id,"alias-${when}_more",test,t101.jpg
254 my $imp = BSE::Importer->new(cfg => $cfg, profile => "alias$when", callback => sub { note @_ });
255 $imp->process($filename);
256 my $testb = BSE::TB::Articles->getByPkey($testa->id);
257 is($testb->linkAlias, "alias-${when}_more", "check alias set correctly");
261 $testa->remove($cfg) if $testa;