]>
Commit | Line | Data |
---|---|---|
57e4a9c7 TC |
1 | #!perl -w |
2 | use strict; | |
3 | use BSE::Test qw(base_url); | |
4 | use File::Spec; | |
5 | use File::Temp; | |
8921ff01 | 6 | use Test::More; |
57e4a9c7 | 7 | |
8921ff01 TC |
8 | BEGIN { |
9 | eval "require Text::CSV;" | |
10 | or plan skip_all => "Text::CSV not available"; | |
11 | } | |
9ef99d41 | 12 | plan tests => 44; |
57e4a9c7 TC |
13 | |
14 | BEGIN { | |
15 | unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules"); | |
16 | } | |
17 | ||
18 | use BSE::Importer; | |
19 | use BSE::API qw(bse_init bse_make_article); | |
20 | ||
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"; | |
24 | ||
25 | my $when = time; | |
26 | ||
27 | my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG); | |
28 | [import profile simple$when] | |
29 | map_title=1 | |
30 | source=CSV | |
31 | target=Article | |
32 | ||
33 | [import profile simpleupdate$when] | |
34 | map_linkAlias=1 | |
35 | map_body=2 | |
3f58d535 TC |
36 | map_file1_file=3 |
37 | map_image1_file=4 | |
57e4a9c7 TC |
38 | source=CSV |
39 | target=Article | |
40 | update_only=1 | |
41 | sep_char=\\t | |
9ef99d41 | 42 | file_path=t/data |
3f58d535 TC |
43 | ignore_missing=0 |
44 | ||
45 | [import profile completefile$when] | |
46 | map_linkAlias=1 | |
47 | map_file1_file=2 | |
48 | map_file1_name=3 | |
49 | map_file1_displayName=4 | |
50 | map_file1_storage=5 | |
51 | map_file1_description=6 | |
52 | map_file1_forSale=7 | |
53 | map_file1_download=8 | |
54 | map_file1_requireUser=9 | |
55 | map_file1_notes=10 | |
56 | map_file1_hide_from_list=11 | |
57 | skiplines=0 | |
9ef99d41 | 58 | file_path=t/data |
3f58d535 TC |
59 | ignore_missing=0 |
60 | update_only=1 | |
61 | source=CSV | |
62 | target=Article | |
63 | ||
64 | [import profile updatefile$when] | |
65 | map_linkAlias=1 | |
66 | map_file1_name=2 | |
67 | map_file1_description=3 | |
68 | skiplines=0 | |
69 | file_path=t | |
70 | ignore_missing=0 | |
71 | update_only=1 | |
72 | source=CSV | |
73 | target=Article | |
74 | ||
75 | [import profile updatefileb$when] | |
76 | map_linkAlias=1 | |
77 | map_file1_name=2 | |
78 | map_file1_file=3 | |
79 | skiplines=0 | |
80 | file_path=t/data | |
81 | ignore_missing=0 | |
82 | update_only=1 | |
83 | source=CSV | |
84 | target=Article | |
85 | ||
1455f602 TC |
86 | [import profile newdup$when] |
87 | map_linkAlias=1 | |
88 | map_title=2 | |
89 | skiplines=0 | |
90 | source=CSV | |
91 | target=Article | |
92 | ||
a6361f2f TC |
93 | [import profile alias$when] |
94 | map_id=1 | |
95 | map_linkAlias=2 | |
96 | map_title=3 | |
97 | source=CSV | |
98 | target=Article | |
99 | code_field=id | |
100 | skiplines=0 | |
101 | update_only=1 | |
102 | use_codes=1 | |
57e4a9c7 TC |
103 | CFG |
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; | |
129 | linkAlias\tbody\tfile1_file\timage1_file | |
57e4a9c7 TC |
130 | "alias$when"\t"This is the body text with multiple lines |
131 | ||
9ef99d41 | 132 | Yes, multiple lines with CSV!"\ttestdata.txt\tt101.jpg |
3f58d535 TC |
133 | EOS |
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 | 159 | EOS |
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" | |
185 | EOS | |
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 | |
212 | EOS | |
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 | |
239 | EOS | |
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 | |
252 | EOS | |
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 | } |