Skip to content

Commit 425020c

Browse files
committed
Merge commit 'refs/changes/35/14735/1' of ssh://gerrit.tuleap.net:29418/tuleap into HEAD
* ssh://gerrit.tuleap.net:29418/tuleap: Add the obsolescence date and the status metdata for link item Change-Id: I57dcfc2a697d92ee7e7e6357216ba52e579b6ba2
2 parents 23a6d9e + 35cb8ac commit 425020c

File tree

6 files changed

+251
-9
lines changed

6 files changed

+251
-9
lines changed

plugins/docman/include/REST/v1/DocmanFoldersResource.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ public function postEmbeds(int $id, DocmanEmbeddedPOSTRepresentation $embeds_rep
357357
/**
358358
* Create new link document
359359
*
360+
* <pre>
361+
* /!\ This route is under construction and will be subject to changes
362+
* </pre>
363+
*
364+
* The format of the obsolescence date is : "YYYY-MM-DD"
365+
*
360366
* @param int $id Id of the parent folder
361367
* @param DocmanLinkPOSTRepresentation $links_representation {@from body}
362368
* {@type \Tuleap\Docman\REST\v1\Links\DocmanLinkPOSTRepresentation}
@@ -392,13 +398,25 @@ public function postLinks(int $id, DocmanLinkPOSTRepresentation $links_represent
392398

393399
$docman_item_creator = DocmanItemCreatorBuilder::build($project);
394400

395-
return $docman_item_creator->createLink(
396-
$parent,
397-
$current_user,
398-
$links_representation,
399-
new \DateTimeImmutable(),
400-
$project
401-
);
401+
try {
402+
return $docman_item_creator->createLink(
403+
$parent,
404+
$current_user,
405+
$links_representation,
406+
new \DateTimeImmutable(),
407+
$project
408+
);
409+
} catch (Metadata\StatusNotFoundException $e) {
410+
throw new RestException(400, $e->getMessage());
411+
} catch (Metadata\ItemStatusUsageMismatchException $e) {
412+
throw new RestException(403, 'The "Status" property is not activated for this item.');
413+
} catch (Metadata\InvalidDateComparisonException $e) {
414+
throw new RestException(400, 'The obsolescence date is before the current date');
415+
} catch (Metadata\InvalidDateTimeFormatException $e) {
416+
throw new RestException(400, 'The date format is incorrect. The format should be YYYY-MM-DD');
417+
} catch (Metadata\ObsoloscenceDateUsageMismatchException $e) {
418+
throw new RestException(403, $e->getMessage());
419+
}
402420
}
403421

404422
private function sendAllowHeadersWithPost()

plugins/docman/include/REST/v1/DocmanItemCreator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ public function createLink(
526526
$project,
527527
$representation->title,
528528
$representation->description,
529-
ItemStatusMapper::ITEM_STATUS_NONE,
530-
ItemRepresentation::OBSOLESCENCE_DATE_NONE,
529+
$representation->status,
530+
$representation->obsolescence_date,
531531
null,
532532
$link_url,
533533
null

plugins/docman/include/REST/v1/Links/DocmanLinkPOSTRepresentation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
namespace Tuleap\Docman\REST\v1\Links;
2424

25+
use Tuleap\Docman\REST\v1\ItemRepresentation;
26+
use Tuleap\Docman\REST\v1\Metadata\ItemStatusMapper;
27+
2528
class DocmanLinkPOSTRepresentation
2629
{
2730
/**
@@ -36,4 +39,12 @@ class DocmanLinkPOSTRepresentation
3639
* @var LinkPropertiesPOSTPATCHRepresentation {@type \Tuleap\Docman\REST\v1\Links\LinkPropertiesPOSTPATCHRepresentation} {@from body}
3740
*/
3841
public $link_properties;
42+
/**
43+
* @var string | null Item status {@from body} {@required false} {@choice none,draft,approved,rejected}
44+
*/
45+
public $status = ItemStatusMapper::ITEM_STATUS_NONE;
46+
/**
47+
* @var string Obsolescence date {@from body} {@required false}
48+
*/
49+
public $obsolescence_date = ItemRepresentation::OBSOLESCENCE_DATE_NONE;
3950
}

plugins/docman/phpunit/REST/v1/DocmanItemCreatorTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,95 @@ public function testCreateEmbeddedDocumentWithStatusAndObsolescenceDate(): void
10011001

10021002
$this->assertSame(12, $created_item_representation->id);
10031003
}
1004+
1005+
public function testCreateLinkDocumentWithStatusAndObsolescenceDate(): void
1006+
{
1007+
$item_creator = new DocmanItemCreator(
1008+
$this->item_factory,
1009+
$this->document_ongoing_upload_retriever,
1010+
$this->document_to_upload_creator,
1011+
$this->creator_visitor,
1012+
$this->empty_file_to_upload_finisher,
1013+
$this->link_validity_checker,
1014+
$this->item_status_mapper,
1015+
$this->metadata_item_status_checker,
1016+
$this->metadata_obsolescence_date_checker,
1017+
$this->metadata_obsolesence_date_retriever
1018+
);
1019+
1020+
$parent_item = \Mockery::mock(\Docman_Item::class);
1021+
$user = \Mockery::mock(\PFUser::class);
1022+
$project = \Mockery::mock(\Project::class);
1023+
$current_time = new \DateTimeImmutable();
1024+
1025+
$post_representation = new DocmanLinkPOSTRepresentation();
1026+
$post_representation->title = 'Link with status and Obsolescence date';
1027+
$post_representation->link_properties = new LinkPropertiesRepresentation();
1028+
$post_representation->link_properties->link_url = 'https://my.example.test';
1029+
$post_representation->status = 'approved';
1030+
$post_representation->obsolescence_date = '2019-10-11';
1031+
1032+
1033+
$this->document_ongoing_upload_retriever->shouldReceive('isThereAlreadyAnUploadOngoing')->andReturns(false);
1034+
$parent_item->shouldReceive('getId')->andReturns(11);
1035+
$user->shouldReceive('getId')->andReturns(222);
1036+
$project->shouldReceive('getID')->andReturns(102);
1037+
1038+
$this->item_status_mapper->shouldReceive('getItemStatusIdFromItemStatusString')
1039+
->andReturn(PLUGIN_DOCMAN_ITEM_STATUS_REJECTED);
1040+
1041+
$this->metadata_obsolescence_date_checker->shouldReceive('checkObsolescenceDateUsage')->once();
1042+
$obsolescence_date_time_stamp = 123456;
1043+
$this->metadata_obsolesence_date_retriever->shouldReceive('getTimeStampOfDate')
1044+
->withArgs(
1045+
[
1046+
$post_representation->obsolescence_date,
1047+
PLUGIN_DOCMAN_ITEM_TYPE_LINK
1048+
]
1049+
)
1050+
->andReturn($obsolescence_date_time_stamp);
1051+
1052+
$this->metadata_obsolescence_date_checker->shouldReceive('checkDateValidity')->once();
1053+
1054+
$this->metadata_item_status_checker->shouldReceive(
1055+
'checkStatusIsNotSetWhenStatusMetadataIsNotAllowed'
1056+
)->andReturn(false);
1057+
1058+
$created_item = \Mockery::mock(\Docman_Link::class);
1059+
$created_item->shouldReceive('getId')->andReturns(12);
1060+
$created_item->shouldReceive('getParentId')->andReturns(11);
1061+
$created_item->makePartial();
1062+
1063+
$this->item_factory
1064+
->shouldReceive('createWithoutOrdering')
1065+
->with(
1066+
'Link with status and Obsolescence date',
1067+
'',
1068+
11,
1069+
PLUGIN_DOCMAN_ITEM_STATUS_REJECTED,
1070+
$obsolescence_date_time_stamp,
1071+
222,
1072+
PLUGIN_DOCMAN_ITEM_TYPE_LINK,
1073+
null,
1074+
'https://my.example.test'
1075+
)
1076+
->once()
1077+
->andReturns($created_item);
1078+
1079+
$this->item_factory->shouldReceive('doesTitleCorrespondToExistingDocument')->andReturn(false);
1080+
1081+
$this->creator_visitor->shouldReceive('visitLink')->once();
1082+
1083+
$this->link_validity_checker->shouldReceive("checkLinkValidity");
1084+
1085+
$created_item_representation = $item_creator->createLink(
1086+
$parent_item,
1087+
$user,
1088+
$post_representation,
1089+
$current_time,
1090+
$project
1091+
);
1092+
1093+
$this->assertSame(12, $created_item_representation->id);
1094+
}
10041095
}

plugins/docman/tests/rest/Docman/DocmanItemsTestFoldersTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,54 @@ public function testPostEmbeddedWithStatusWhenStatusIsNotAllowedForProject(int $
565565
$this->assertEquals(403, $response->getStatusCode());
566566
}
567567

568+
/**
569+
* @depends testGetRootId
570+
*/
571+
public function testPostLinkWithStatusWhenStatusIsNotAllowedForProject(int $root_id): void
572+
{
573+
$headers = ['Content-Type' => 'application/json'];
574+
$link_properties = ['link_url' => 'https://turfu.example.test'];
575+
$query = json_encode(
576+
[
577+
'title' => 'To the future 2',
578+
'description' => 'A description',
579+
'link_properties' => $link_properties,
580+
'status' => 'approved'
581+
]
582+
);
583+
584+
$response = $this->getResponseByName(
585+
DocmanDataBuilder::DOCMAN_REGULAR_USER_NAME,
586+
$this->client->post('docman_folders/' . $root_id . "/links", $headers, $query)
587+
);
588+
589+
$this->assertEquals(403, $response->getStatusCode());
590+
}
591+
592+
/**
593+
* @depends testGetRootId
594+
*/
595+
public function testPostLinkWithObsolescenceDateWhenObsolescenceDateIsNotAllowedForProject(int $root_id): void
596+
{
597+
$headers = ['Content-Type' => 'application/json'];
598+
$link_properties = ['link_url' => 'https://turfu.example.test'];
599+
$query = json_encode(
600+
[
601+
'title' => 'To the future 3, the return',
602+
'description' => 'A description',
603+
'link_properties' => $link_properties,
604+
'obsolescence_date' => '3000-08-08'
605+
]
606+
);
607+
608+
$response = $this->getResponseByName(
609+
DocmanDataBuilder::DOCMAN_REGULAR_USER_NAME,
610+
$this->client->post('docman_folders/' . $root_id . "/links", $headers, $query)
611+
);
612+
613+
$this->assertEquals(403, $response->getStatusCode());
614+
}
615+
568616
/**
569617
* Find first item in given array of items which has given title.
570618
* @return array|null Found item. null otherwise.

plugins/docman/tests/rest/docmanWithActivatedMetadata/DocmanHardcodedMetadataTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,80 @@ public function testPostEmbeddedFileThrows400IfTheDateIsNotWellFormatted(array $
246246
$this->assertEquals(400, $response->getStatusCode());
247247
}
248248

249+
/**
250+
* @depends testGetRootId
251+
*/
252+
public function testPostLinkWithStatusAndObsolescenceDate(int $root_id): void
253+
{
254+
$headers = ['Content-Type' => 'application/json'];
255+
$link_properties = ['link_url' => 'https://turfu.example.test'];
256+
$query = json_encode(
257+
[
258+
'title' => 'To the future 2',
259+
'description' => 'A description',
260+
'link_properties' => $link_properties,
261+
'status' => 'approved',
262+
'obsolescence_date' => '3000-08-25'
263+
]
264+
);
265+
266+
$response = $this->getResponseByName(
267+
DocmanDataBuilder::DOCMAN_REGULAR_USER_NAME,
268+
$this->client->post('docman_folders/' . $root_id . "/links", $headers, $query)
269+
);
270+
271+
$this->assertEquals(201, $response->getStatusCode());
272+
}
273+
274+
/**
275+
* @depends testGetRootId
276+
*/
277+
public function testPostLinkWithStatusThrows400IfThereIsABadStatus(int $root_id): void
278+
{
279+
$headers = ['Content-Type' => 'application/json'];
280+
$link_properties = ['link_url' => 'https://turfu.example.test'];
281+
$query = json_encode(
282+
[
283+
'title' => 'To the future 2',
284+
'description' => 'A description',
285+
'link_properties' => $link_properties,
286+
'status' => 'approveddg,sigvjzeg',
287+
'obsolescence_date' => '3000-08-25'
288+
]
289+
);
290+
291+
$response = $this->getResponseByName(
292+
DocmanDataBuilder::DOCMAN_REGULAR_USER_NAME,
293+
$this->client->post('docman_folders/' . $root_id . "/links", $headers, $query)
294+
);
295+
296+
$this->assertEquals(400, $response->getStatusCode());
297+
}
298+
299+
/**
300+
* @depends testGetRootId
301+
*/
302+
public function testPostLinkThrows400IfTheDateIsNotWellFormatted(int $root_id): void
303+
{
304+
$headers = ['Content-Type' => 'application/json'];
305+
$link_properties = ['link_url' => 'https://turfu.example.test'];
306+
$query = json_encode(
307+
[
308+
'title' => 'To the future 3',
309+
'description' => 'A description',
310+
'link_properties' => $link_properties,
311+
'status' => 'approveddg,sigvjzeg',
312+
'obsolescence_date' => '3000-08-25884444'
313+
]
314+
);
315+
316+
$response = $this->getResponseByName(
317+
DocmanDataBuilder::DOCMAN_REGULAR_USER_NAME,
318+
$this->client->post('docman_folders/' . $root_id . "/links", $headers, $query)
319+
);
320+
321+
$this->assertEquals(400, $response->getStatusCode());
322+
}
249323

250324
/**
251325
* Find first item in given array of items which has given title.

0 commit comments

Comments
 (0)