@@ -1487,4 +1487,146 @@ public function testDefaultExpireDate() {
14871487 $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'no ' );
14881488
14891489 }
1490+
1491+ public function datesProvider () {
1492+ $ date = new \DateTime ();
1493+ $ date ->add (new \DateInterval ('P5D ' ));
1494+
1495+ $ year = (int )$ date ->format ('Y ' );
1496+
1497+ return [
1498+ [$ date ->format ('Y-m-d ' ), true ],
1499+ [$ year +1 . '-1-1 ' , false ],
1500+ [$ date ->format ('Y-m-dTH:m ' ), false ],
1501+ ['abc ' , false ],
1502+ [$ date ->format ('Y-m-d ' ) . 'xyz ' , false ],
1503+ ];
1504+ }
1505+
1506+ /**
1507+ * Make sure only ISO 8601 dates are accepted
1508+ *
1509+ * @dataProvider datesProvider
1510+ */
1511+ public function testPublicLinkExpireDate ($ date , $ valid ) {
1512+ $ _POST ['path ' ] = $ this ->folder ;
1513+ $ _POST ['shareType ' ] = \OCP \Share::SHARE_TYPE_LINK ;
1514+ $ _POST ['expireDate ' ] = $ date ;
1515+
1516+ $ result = \OCA \Files_Sharing \API \Local::createShare ([]);
1517+
1518+ if ($ valid === false ) {
1519+ $ this ->assertFalse ($ result ->succeeded ());
1520+ $ this ->assertEquals (404 , $ result ->getStatusCode ());
1521+ $ this ->assertEquals ('Invalid Date. Format must be YYYY-MM-DD. ' , $ result ->getMeta ()['message ' ]);
1522+ return ;
1523+ }
1524+
1525+ $ this ->assertTrue ($ result ->succeeded ());
1526+
1527+ $ data = $ result ->getData ();
1528+ $ this ->assertTrue (is_string ($ data ['token ' ]));
1529+
1530+ // check for correct link
1531+ $ url = \OC ::$ server ->getURLGenerator ()->getAbsoluteURL ('/index.php/s/ ' . $ data ['token ' ]);
1532+ $ this ->assertEquals ($ url , $ data ['url ' ]);
1533+
1534+
1535+ $ share = $ this ->getShareFromId ($ data ['id ' ]);
1536+ $ items = \OCP \Share::getItemShared ('file ' , $ share ['item_source ' ]);
1537+ $ this ->assertTrue (!empty ($ items ));
1538+
1539+ $ item = reset ($ items );
1540+ $ this ->assertTrue (is_array ($ item ));
1541+ $ this ->assertEquals ($ date , substr ($ item ['expiration ' ], 0 , 10 ));
1542+
1543+ $ fileinfo = $ this ->view ->getFileInfo ($ this ->folder );
1544+ \OCP \Share::unshare ('folder ' , $ fileinfo ['fileid ' ], \OCP \Share::SHARE_TYPE_LINK , null );
1545+ }
1546+
1547+ public function testCreatePublicLinkExpireDateValid () {
1548+ $ config = \OC ::$ server ->getConfig ();
1549+
1550+ // enforce expire date, by default 7 days after the file was shared
1551+ $ config ->setAppValue ('core ' , 'shareapi_default_expire_date ' , 'yes ' );
1552+ $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'yes ' );
1553+
1554+ $ date = new \DateTime ();
1555+ $ date ->add (new \DateInterval ('P5D ' ));
1556+
1557+ $ _POST ['path ' ] = $ this ->folder ;
1558+ $ _POST ['shareType ' ] = \OCP \Share::SHARE_TYPE_LINK ;
1559+ $ _POST ['expireDate ' ] = $ date ->format ('Y-m-d ' );
1560+
1561+ $ result = \OCA \Files_Sharing \API \Local::createShare ([]);
1562+
1563+ $ this ->assertTrue ($ result ->succeeded ());
1564+
1565+ $ data = $ result ->getData ();
1566+ $ this ->assertTrue (is_string ($ data ['token ' ]));
1567+
1568+ // check for correct link
1569+ $ url = \OC ::$ server ->getURLGenerator ()->getAbsoluteURL ('/index.php/s/ ' . $ data ['token ' ]);
1570+ $ this ->assertEquals ($ url , $ data ['url ' ]);
1571+
1572+
1573+ $ share = $ this ->getShareFromId ($ data ['id ' ]);
1574+ $ items = \OCP \Share::getItemShared ('file ' , $ share ['item_source ' ]);
1575+ $ this ->assertTrue (!empty ($ items ));
1576+
1577+ $ item = reset ($ items );
1578+ $ this ->assertTrue (is_array ($ item ));
1579+ $ this ->assertEquals ($ date ->format ('Y-m-d ' ), substr ($ item ['expiration ' ], 0 , 10 ));
1580+
1581+ $ fileinfo = $ this ->view ->getFileInfo ($ this ->folder );
1582+ \OCP \Share::unshare ('folder ' , $ fileinfo ['fileid ' ], \OCP \Share::SHARE_TYPE_LINK , null );
1583+
1584+ $ config ->setAppValue ('core ' , 'shareapi_default_expire_date ' , 'no ' );
1585+ $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'no ' );
1586+ }
1587+
1588+ public function testCreatePublicLinkExpireDateInvalidFuture () {
1589+ $ config = \OC ::$ server ->getConfig ();
1590+
1591+ // enforce expire date, by default 7 days after the file was shared
1592+ $ config ->setAppValue ('core ' , 'shareapi_default_expire_date ' , 'yes ' );
1593+ $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'yes ' );
1594+
1595+ $ date = new \DateTime ();
1596+ $ date ->add (new \DateInterval ('P8D ' ));
1597+
1598+ $ _POST ['path ' ] = $ this ->folder ;
1599+ $ _POST ['shareType ' ] = \OCP \Share::SHARE_TYPE_LINK ;
1600+ $ _POST ['expireDate ' ] = $ date ->format ('Y-m-d ' );
1601+
1602+ $ result = \OCA \Files_Sharing \API \Local::createShare ([]);
1603+
1604+ $ this ->assertFalse ($ result ->succeeded ());
1605+ $ this ->assertEquals (404 , $ result ->getStatusCode ());
1606+ $ this ->assertEquals ('Cannot set expiration date. Shares cannot expire later than 7 after they have been shared ' , $ result ->getMeta ()['message ' ]);
1607+
1608+ $ config ->setAppValue ('core ' , 'shareapi_default_expire_date ' , 'no ' );
1609+ $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'no ' );
1610+ }
1611+
1612+ public function testCreatePublicLinkExpireDateInvalidPast () {
1613+ $ config = \OC ::$ server ->getConfig ();
1614+
1615+ $ date = new \DateTime ();
1616+ $ date ->sub (new \DateInterval ('P8D ' ));
1617+
1618+ $ _POST ['path ' ] = $ this ->folder ;
1619+ $ _POST ['shareType ' ] = \OCP \Share::SHARE_TYPE_LINK ;
1620+ $ _POST ['expireDate ' ] = $ date ->format ('Y-m-d ' );
1621+
1622+ $ result = \OCA \Files_Sharing \API \Local::createShare ([]);
1623+
1624+ $ this ->assertFalse ($ result ->succeeded ());
1625+ $ this ->assertEquals (404 , $ result ->getStatusCode ());
1626+ $ this ->assertEquals ('Cannot set expiration date. Expiration date is in the past ' , $ result ->getMeta ()['message ' ]);
1627+
1628+ $ config ->setAppValue ('core ' , 'shareapi_default_expire_date ' , 'no ' );
1629+ $ config ->setAppValue ('core ' , 'shareapi_enforce_expire_date ' , 'no ' );
1630+ }
1631+
14901632}
0 commit comments