use the supplied name of the image file for working out the stored name
[bse.git] / t / 130-importer / 020-article.t
CommitLineData
57e4a9c7
TC
1#!perl -w
2use strict;
3use BSE::Test qw(base_url);
4use File::Spec;
5use File::Temp;
8921ff01 6use Test::More;
57e4a9c7 7
8921ff01
TC
8BEGIN {
9 eval "require Text::CSV;"
10 or plan skip_all => "Text::CSV not available";
11}
9ef99d41 12plan tests => 44;
57e4a9c7
TC
13
14BEGIN {
15 unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules");
16}
17
18use BSE::Importer;
19use BSE::API qw(bse_init bse_make_article);
20
21my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin");
22ok(bse_init($base_cgi), "initialize api")
23 or print "# failed to bse_init in $base_cgi\n";
24
25my $when = time;
26
27my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG);
28[import profile simple$when]
29map_title=1
30source=CSV
31target=Article
32
33[import profile simpleupdate$when]
34map_linkAlias=1
35map_body=2
3f58d535
TC
36map_file1_file=3
37map_image1_file=4
57e4a9c7
TC
38source=CSV
39target=Article
40update_only=1
41sep_char=\\t
9ef99d41 42file_path=t/data
3f58d535
TC
43ignore_missing=0
44
45[import profile completefile$when]
46map_linkAlias=1
47map_file1_file=2
48map_file1_name=3
49map_file1_displayName=4
50map_file1_storage=5
51map_file1_description=6
52map_file1_forSale=7
53map_file1_download=8
54map_file1_requireUser=9
55map_file1_notes=10
56map_file1_hide_from_list=11
57skiplines=0
9ef99d41 58file_path=t/data
3f58d535
TC
59ignore_missing=0
60update_only=1
61source=CSV
62target=Article
63
64[import profile updatefile$when]
65map_linkAlias=1
66map_file1_name=2
67map_file1_description=3
68skiplines=0
69file_path=t
70ignore_missing=0
71update_only=1
72source=CSV
73target=Article
74
75[import profile updatefileb$when]
76map_linkAlias=1
77map_file1_name=2
78map_file1_file=3
79skiplines=0
80file_path=t/data
81ignore_missing=0
82update_only=1
83source=CSV
84target=Article
85
1455f602
TC
86[import profile newdup$when]
87map_linkAlias=1
88map_title=2
89skiplines=0
90source=CSV
91target=Article
92
a6361f2f
TC
93[import profile alias$when]
94map_id=1
95map_linkAlias=2
96map_title=3
97source=CSV
98target=Article
99code_field=id
100skiplines=0
101update_only=1
102use_codes=1
57e4a9c7
TC
103CFG
104
105{
106 my @added;
107
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;
111
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");
115
116 END {
117 $_->remove($cfg) for @added;
118 }
119}
120
121{
122 my $testa = bse_make_article(cfg => $cfg, title => "test updates",
123 linkAlias => "alias$when");
124
3f58d535
TC
125 {
126 my $fh = File::Temp->new;
127 my $filename = $fh->filename;
128 print $fh <<EOS;
129linkAlias\tbody\tfile1_file\timage1_file
57e4a9c7
TC
130"alias$when"\t"This is the body text with multiple lines
131
9ef99d41 132Yes, multiple lines with CSV!"\ttestdata.txt\tt101.jpg
3f58d535
TC
133EOS
134 close $fh;
135 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simpleupdate$when", callback => sub { note @_ });
136 $imp->process($filename);
ab004bbb 137 my $testb = BSE::TB::Articles->getByPkey($testa->id);
3f58d535
TC
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");
144
9ef99d41
TC
145 cmp_ok($images[0]->image, '!~', 'data', "check we don't pass full path as basename");
146
3f58d535
TC
147 my @files = $testb->files;
148 is(@files, 1, "should be 1 file");
9ef99d41
TC
149 is($files[0]->displayName, "testdata.txt", "check display name");
150 is(-s $files[0]->full_filename, -s "t/data/testdata.txt", "check size");
3f58d535
TC
151 }
152
153 SKIP:
154 {
155 my $fh = File::Temp->new;
156 my $filename = $fh->filename;
157 print $fh <<EOS;
9ef99d41 158"alias$when",testdata.txt,test,"A Test File.txt",local,"A test file from BSE",1,1,1,"Some Notes",1
57e4a9c7 159EOS
3f58d535
TC
160 close $fh;
161 my $imp = BSE::Importer->new(cfg => $cfg, profile => "completefile$when", callback => sub { note @_ });
162 $imp->process($filename);
ab004bbb 163 my $testb = BSE::TB::Articles->getByPkey($testa->id);
3f58d535
TC
164
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;
9ef99d41 168 is(-s $file->full_filename, -s "t/data/testdata.txt", "check size");
3f58d535
TC
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");
177 }
178
179 SKIP:
180 {
181 my $fh = File::Temp->new;
182 my $filename = $fh->filename;
183 print $fh <<EOS;
184"alias$when",test,"New description"
185EOS
186 close $fh;
187 my $imp = BSE::Importer->new(cfg => $cfg, profile => "updatefile$when", callback => sub { note @_ });
188 $imp->process($filename);
ab004bbb 189 my $testb = BSE::TB::Articles->getByPkey($testa->id);
3f58d535
TC
190
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
9ef99d41 196 is(-s $file->full_filename, -s "t/data/testdata.txt", "check size");
3f58d535
TC
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");
204 }
205
206 SKIP:
207 {
208 my $fh = File::Temp->new;
209 my $filename = $fh->filename;
210 print $fh <<EOS;
211"alias$when",test,t101.jpg
212EOS
213 close $fh;
214 my $imp = BSE::Importer->new(cfg => $cfg, profile => "updatefileb$when", callback => sub { note @_ });
215 $imp->process($filename);
ab004bbb 216 my $testb = BSE::TB::Articles->getByPkey($testa->id);
3f58d535
TC
217
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");
231 }
57e4a9c7 232
1455f602
TC
233 { # fail to duplicate a link alias
234 my $fh = File::Temp->new;
235 my $filename = $fh->filename;
236 my $id = $testa->id;
237 print $fh <<EOS;
238"alias$when",test,t101.jpg
239EOS
240 close $fh;
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");
244 }
245
a6361f2f
TC
246 { # don't strip - and _ from linkAlias
247 my $fh = File::Temp->new;
248 my $filename = $fh->filename;
249 my $id = $testa->id;
250 print $fh <<EOS;
251$id,"alias-${when}_more",test,t101.jpg
252EOS
253 close $fh;
254 my $imp = BSE::Importer->new(cfg => $cfg, profile => "alias$when", callback => sub { note @_ });
255 $imp->process($filename);
ab004bbb 256 my $testb = BSE::TB::Articles->getByPkey($testa->id);
a6361f2f
TC
257 is($testb->linkAlias, "alias-${when}_more", "check alias set correctly");
258 }
259
57e4a9c7
TC
260 END {
261 $testa->remove($cfg) if $testa;
262 }
263}