File Coverage

inc/WebService/Cmis/Test/Document.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package WebService::Cmis::Test::Document;
2 1     1   662 use base qw(WebService::Cmis::Test);
  1         2  
  1         506  
3              
4             use strict;
5             use warnings;
6              
7             use Error qw(:try);
8             use Test::More;
9             use WebService::Cmis qw(:collections :utils :relations :namespaces :contenttypes);
10              
11             sub _getParents {
12             my $obj = shift;
13              
14             note("called _getParents for obj=".$obj->getName.", id=".$obj->getId.", path=".($obj->getPath||''));
15              
16             my $parents = $obj->getObjectParents;
17              
18             my @parents = ();
19             if ($parents->isa("WebService::Cmis::AtomFeed")) {
20             note("nr parents: ".$parents->getSize);
21             push @parents, $_ while $_ = $parents->getNext;
22             } else {
23             push @parents, $parents;
24             }
25              
26             return @parents;
27             }
28              
29             sub _saveFile {
30             my ($name, $text) = @_;
31             my $FILE;
32             unless (open($FILE, '>', $name)) {
33             die "Can't create file $name - $!\n";
34             }
35             print $FILE $text;
36             close($FILE);
37             }
38              
39             sub test_Document_getAllVersions : Tests {
40             my $this = shift;
41             my $repo = $this->getRepository;
42             my $root = $repo->getRootFolder;
43             my $resultSet = $root->getDescendants(depth=>2);
44              
45             ok(defined $resultSet) or diag("can't fetch results");
46              
47             note("found ".$resultSet->getSize." documents in root collection");
48              
49             while(my $obj = $resultSet->getNext) {
50             next unless $obj->isa("WebService::Cmis::Document");
51              
52             note("versions in ".$obj->getId.", url=".$obj->getSelfLink);
53             my $allVersions = $obj->getAllVersions;
54             ok(defined $allVersions);
55             ok($allVersions->getSize > 0) or diag("no versions for ".$obj->toString);
56              
57             while(my $version = $allVersions->getNext) {
58             note("version=".$version->toString);
59             my $props = $version->getProperties;
60              
61             # SMELL: which of these are standard, which are nice-to-haves by alfresco?
62             foreach my $propId (qw(cmis:contentStreamFileName cmis:name
63             cmis:baseTypeId cmis:isImmutable cmis:isLatestMajorVersion cmis:changeToken
64             cmis:isVersionSeriesCheckedOut cmis:objectTypeId cmis:createdBy
65             cmis:versionSeriesId cmis:versionSeriesCheckedOutBy cmis:lastModificationDate
66             cmis:versionSeriesCheckedOutId cmis:isLatestVersion cmis:objectId
67             cmis:checkinComment cmis:versionLabel cmis:creationDate cmis:contentStreamId
68             cmis:contentStreamLength cmis:contentStreamMimeType cmis:lastModifiedBy
69             cmis:isMajorVersion)) {
70             note(" $propId=".($props->{$propId}->getValue||''));
71             ok(defined $props->{$propId}) or diag("property $propId not defined");
72             }
73             }
74             }
75             }
76              
77             sub test_Document_checkOut_checkIn : Tests {
78             my $this = shift;
79              
80             my $repo = $this->getRepository;
81             my $obj = $this->getTestDocument;
82              
83             my $isCheckedOut = $obj->isCheckedOut;
84             note("isCheckedout=$isCheckedOut");
85             is($isCheckedOut, 0) or diag("test document is checked out");
86              
87             note("before checking out, id=".$obj->getId." version=".($obj->getProperty("cmis:versionLabel")||''));
88              
89             $obj->checkOut;
90             $isCheckedOut = $obj->isCheckedOut;
91             note("isCheckedout=$isCheckedOut");
92             is($isCheckedOut, 1) or diag("test document is NOT checked out");
93              
94             note("after checking out, id=".$obj->getId." version=".($obj->getProperty("cmis:versionLabel")||''));
95              
96             my $checkedOutBy = $obj->getCheckedOutBy;
97             note("checkedOutBy=$checkedOutBy");
98             ok(defined $checkedOutBy) or diag("no information checked out by");
99            
100             note("checking in");
101             $obj->checkIn("this is a test checkin time=".time, major=>'true');
102              
103             $obj->getLatestVersion;
104             note("finally id=".$obj->getId." version=".($obj->getProperty("cmis:versionLabel")||''));
105            
106             $this->deleteTestDocument;
107             }
108              
109             sub test_Document_getContentStream : Test(2) {
110             my $this = shift;
111              
112             my $obj = $this->getTestDocument;
113              
114             my $content = $obj->getContentStream;
115             ok(defined $content);
116              
117             my $name = $obj->getName;
118             ok(defined $name);
119             note("name=$name");
120             _saveFile("/tmp/downloaded_$name", $content);
121              
122             }
123              
124             sub test_Document_setContentStream : Test(6) {
125             my $this = shift;
126              
127             my $repo = $this->getRepository;
128             my $contentStreamUpdatability = $repo->getCapabilities->{'ContentStreamUpdatability'};
129              
130             note("contentStreamUpdatability=$contentStreamUpdatability");
131              
132             SKIP: {
133             skip "setContentStream not supported", 6, if $contentStreamUpdatability eq 'none';
134              
135             my $obj = $this->getTestDocument;
136             my $id = $obj->getId;
137              
138             note("before id=$id");
139              
140             my $versionLabel = $obj->getProperty("cmis:versionLabel");
141             ok(defined $versionLabel);
142             note("versionLabel=$versionLabel");
143              
144             $obj->setContentStream(contentFile => $this->{testFile});
145              
146             $id = $obj->getId;
147             note("after id=$id");
148              
149             ok(defined $obj->{xmlDoc});
150              
151             #print STDERR "xmlDoc=".$obj->{xmlDoc}->toString(1)."\n";
152              
153             my $updatedVersionLabel = $obj->getProperty("cmis:versionLabel");
154             ok(defined $updatedVersionLabel);
155             note("versionLabel=$versionLabel, updatedVersionLabel=" . ($updatedVersionLabel || 'undef'));
156             ok($contentStreamUpdatability ne 'pwconly' || $versionLabel ne $updatedVersionLabel) or
157             diag("should have created a new version when updating the content stream");
158              
159             my $contentStreamMimeType = $obj->getProperty("cmis:contentStreamMimeType");
160              
161             #print STDERR "contentStreamMimeType=$contentStreamMimeType\n";
162             is($contentStreamMimeType, "image/jpeg");
163              
164             $obj->getLatestVersion;
165             my $latestVersionLabel = $obj->getProperty("cmis:versionLabel");
166              
167             #print STDERR "xmlDoc=".$obj->{xmlDoc}->toString(1)."\n";
168             note("latestVersionLabel=$latestVersionLabel");
169             is($updatedVersionLabel, $latestVersionLabel);
170              
171             $this->deleteTestDocument;
172             }
173             }
174              
175             sub test_Document_getContentLink : Test {
176             my $this = shift;
177              
178             my $obj = $this->getTestDocument;
179             my $contentLink = $obj->getContentLink;
180             note("content-link=$contentLink");
181             ok(defined $contentLink) or diag("can't get content link for test file");
182              
183             }
184              
185             sub test_Document_moveTo : Test(5) {
186             my $this = shift;
187              
188             my $repo = $this->getRepository;
189             my $obj = $this->getTestDocument("source");
190              
191             my ($parent1) = _getParents($obj);
192             my $sourcePath = $parent1->getPath."/".$obj->getName;
193             note("sourcePath=".$sourcePath);
194             note("parents: ".join(", ", map($_->getName, _getParents($obj))));
195              
196             my $targetFolder = $this->getTestFolder("target");
197             my $targetPath = $targetFolder->getPath."/".$obj->getName;
198             note("targetPath=$targetPath");
199             isnt($targetPath, $sourcePath);
200              
201             $obj->moveTo($targetFolder);
202              
203             my ($parent2) = _getParents($obj);
204             note("parents: ".join(", ", map($_->getName, _getParents($obj))));
205              
206             is(1, scalar(_getParents($obj))) or diag("not the same number of parents");
207             isnt($parent1->getId, $parent2->getId) or diag("should have changed folder");
208              
209             my $result = $repo->getObjectByPath($sourcePath);
210             ok(!defined $result) or diag("document should NOT be located in source folder anymore");
211              
212             $result = $repo->getObjectByPath($targetPath);
213             ok(defined $result) or diag("document should be located in target folder");
214             }
215              
216             sub test_Document_move : Test(4) {
217             my $this = shift;
218             my $repo = $this->getRepository;
219              
220             my $obj = $this->getTestDocument("source");
221             my $name = $obj->getName;
222             note("name=$name");
223              
224             my $targetFolder = $this->getTestFolder("target");
225             my $targetPath = $targetFolder->getPath."/".$name;
226            
227             my ($sourceFolder) = _getParents($obj);
228             ok(defined $sourceFolder);
229              
230             my $sourcePath = $sourceFolder->getPath."/".$name;
231              
232             note("targetPath=$targetPath, sourcePath=$sourcePath");
233             isnt($targetPath, $sourcePath);
234             $obj->moveTo($targetFolder);
235              
236             #find the document at two paths now
237             my $test = $repo->getObjectByPath($targetPath);
238             ok(defined $test) or diag("document not found at target location");
239              
240             $test = $repo->getObjectByPath($sourcePath);
241             ok(!defined $test) or diag("document still found at source location");
242              
243             $this->deleteTestDocument("source");
244             }
245              
246             sub test_Document_unfile : Tests {
247             my $this = shift;
248             my $repo = $this->getRepository;
249              
250             my $exceptionOk = 0;
251             my $obj = $this->getTestDocument;
252             my $error;
253              
254             try {
255             $obj->unfile;
256             } catch WebService::Cmis::NotSupportedException with {
257             $error = shift;
258             is($error, "This repository does not support unfiling");
259             $exceptionOk = 1;
260             };
261             return $error if $exceptionOk;
262              
263             my $unfiledDocs = $repo->getUnfiledDocs;
264             ok(defined $unfiledDocs) or diag("can't get unfiled docs");
265              
266             note("found ".$unfiledDocs->getSize." unfiled document(s)");
267              
268             while(my $obj = $unfiledDocs->getNext) {
269             note("name=".$obj->getName.", id=".$obj->getId.", url=".$obj->getSelfLink);
270             isa_ok($obj, 'WebService::Cmis::Document');
271             }
272              
273             }
274              
275             sub test_Document_getRenditions : Tests {
276             my $this = shift;
277              
278             my $repo = $this->getRepository;
279             my $vendorName = $repo->getRepositoryInfo->{vendorName};
280             my $productVersion = $repo->getRepositoryInfo->{productVersion};
281              
282             SKIP: {
283             skip "renditions strange in Alfresco 4.2.0", 1, if $vendorName eq 'Alfresco' && $productVersion =~ /^4\.2\.0/;
284              
285             my $obj = $this->getTestDocument;
286              
287             #print STDERR "xmlDoc=".$obj->{xmlDoc}->toString(1)."\n";
288              
289             my $renditions = $obj->getRenditions;
290             ok(defined $renditions);
291             note("renditions:");
292             foreach my $rendition (values %$renditions) {
293             ok(defined $rendition);
294             note("rendition properties:" . join(", ", sort keys %$rendition));
295             ok(defined $rendition->{streamId});
296             ok(defined $rendition->{mimetype});
297             ok(defined $rendition->{kind});
298             my @info = ();
299             foreach my $key (keys %$rendition) {
300             push @info, " $key=$rendition->{$key}";
301             }
302             note(join("\n", @info));
303             }
304             }
305             }
306              
307             sub test_Document_getRenditionLink : Tests {
308             my $this = shift;
309              
310             SKIP: {
311             skip "unclear when this is implemented by repos";
312              
313             my $obj = $this->getTestDocument;
314             my $link = $obj->getRenditionLink(kind=>"thumbnail");
315             #the server might delay thumbnail creation beyond this test
316             #ok(defined $link);
317             #note("thumbnail=$link");
318              
319             $link = $obj->getRenditionLink(mimetype=>"Image");
320             ok(defined $link);
321             note("image=$link");
322              
323             $link = $obj->getRenditionLink(mimetype=>"Image", width=>16);
324             ok(defined $link) || diag("no image/16 rendition");
325             note("image,16=$link");
326              
327             # not implemetned by some repos
328             #
329             # $link = $obj->getRenditionLink(mimetype=>"Image", width=>32);
330             # ok(defined $link) || diag("no image/32 rendition");
331             # note("image,32=".($link||'undef'));
332              
333             # $link = $obj->getRenditionLink(kind=>"icon", height=>16);
334             # ok(defined $link) || diag("no icon/16 rendition");
335             # note("icon=".($link||'undef'));
336              
337             $link = $obj->getRenditionLink(kind=>"icon", height=>11234020);
338             ok(!defined $link);
339             }
340             }
341              
342             sub test_Document_getLatestVersion : Test(9) {
343             my $this = shift;
344              
345             my $repo = $this->getRepository;
346              
347             $this->deleteTestDocument;
348             my $doc = $this->getTestDocument;
349              
350             my $versionLabel = $doc->getProperty("cmis:versionLabel");
351             note("versionLabel=$versionLabel");
352             is($versionLabel, "1.0");
353              
354             note("before checkout id=".$doc->getId);
355              
356             my $beforeCheckedOutDocs = $repo->getCheckedOutDocs;
357             note("before checkout size=".$beforeCheckedOutDocs->getSize);
358              
359             my $checkedOutDocs = $repo->getCheckedOutDocs;
360             while (my $entry = $checkedOutDocs->getNext) {
361             note("checked doc id=".$entry->getId);
362             }
363              
364             $doc->checkOut;
365              
366             $checkedOutDocs = $repo->getCheckedOutDocs;
367             note("after checkout id=".$doc->getId);
368             note("after checkout size=".$checkedOutDocs->getSize);
369             while (my $entry = $checkedOutDocs->getNext) {
370             note("checked doc id=".$entry->getId." version=".($entry->getProperty("cmis:versionLabel")||''));
371             }
372              
373             is($checkedOutDocs->getSize, $beforeCheckedOutDocs->getSize+1) or diag("checked out queue should be increasing");
374              
375             $doc->checkIn("this is a major checkin time=".time);
376              
377             note("after checkin id=".$doc->getId);
378              
379             $checkedOutDocs = $repo->getCheckedOutDocs;
380             while (my $entry = $checkedOutDocs->getNext) {
381             note("checked doc id=".$entry->getId);
382             }
383              
384             is($checkedOutDocs->getSize, $beforeCheckedOutDocs->getSize) or diag("expected the checkedout queue to be the same as before");
385              
386             $doc->getLatestVersion();
387              
388             my $latestVersionDocId = $doc->getId;
389             note("latestVersionDocId=$latestVersionDocId");
390              
391             my $isLatestMajorVersion = $doc->getProperty("cmis:isLatestMajorVersion");
392             note("isLatestMajorVersion=$isLatestMajorVersion");
393             ok($isLatestMajorVersion);
394            
395             my $isLatestVersion = $doc->getProperty("cmis:isLatestVersion");
396             note("isLatestVersion=$isLatestVersion");
397             ok($isLatestVersion);
398            
399             $versionLabel = $doc->getProperty("cmis:versionLabel");
400             note("latest versionLabel=$versionLabel");
401             is("2.0", $versionLabel);
402            
403             $doc->checkOut;
404             $doc->checkIn("this is a minor test checkin time=".time, major=>0);
405            
406             $doc->getLatestVersion;
407             $versionLabel = $doc->getProperty("cmis:versionLabel");
408             note("latest versionLabel=$versionLabel");
409             is("2.1", $versionLabel);
410              
411             $doc->getLatestVersion(major=>1);
412             $versionLabel = $doc->getProperty("cmis:versionLabel");
413             note("latest major versionLabel=$versionLabel");
414             is($versionLabel, "2.0");
415              
416             is($repo->getCheckedOutDocs->getSize, $beforeCheckedOutDocs->getSize) or diag("checked out queue the same as before");
417              
418             $this->deleteTestDocument;
419             }
420              
421             sub test_Document_checkOut_cancelCheckOut : Test(6) {
422             # SMELL: skip some when there is no support for pwc
423              
424             my $this = shift;
425              
426             my $repo = $this->getRepository;
427             my $obj = $this->getTestDocument;
428              
429             my $id = $obj->getId;
430             note("id=$id");
431            
432             my $pwc = $obj->checkOut;
433             my $pwcId = $pwc->getId;
434             note("pwcId=$pwcId");
435             ok($repo->getObject($pwcId));
436              
437             my $pwc2 = $obj->getPrivateWorkingCopy;
438             ok(defined $pwc2);
439             my $pwcId2 = $pwc2->getId;
440             note("pwcId2=$pwcId2");
441             ok($repo->getObject($pwcId2));
442             is($pwcId2, $pwcId);
443            
444             $obj->cancelCheckOut;
445            
446             $id = $obj->getId;
447             note("id=".$id);
448             ok($repo->getObject($id));
449              
450             $pwc = $obj->getPrivateWorkingCopy;
451             ok(!defined $pwc);
452             }
453              
454             1;