$userStatusManager = $this->createMock(IUserStatusManager::class);
$previewManager = $this->createMock(IPreview::class);
$dateTimeZone = $this->createMock(IDateTimeZone::class);
+ $dateTimeZone->method('getTimeZone')->willReturn(new \DateTimeZone(date_default_timezone_get()));
return new ShareAPIController(
self::APP_NAME,
$config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
$dateWithinRange = new \DateTime();
- $dateWithinRange->setTime(0, 0, 0);
- $dateWithinRange->add(new \DateInterval('P5D'));
+ $dateWithinRange->add(new \DateInterval('P6D'));
+
$dateOutOfRange = new \DateTime();
- $dateOutOfRange->setTime(0, 0, 0);
$dateOutOfRange->add(new \DateInterval('P8D'));
// update expire date to a valid value
$share1 = $this->shareManager->getShareById($share1->getFullId());
// date should be changed
+ $dateWithinRange->setTime(0, 0, 0);
+ $dateWithinRange->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$this->assertEquals($dateWithinRange, $share1->getExpirationDate());
// update expire date to a value out of range
public function datesProvider() {
$date = new \DateTime();
+ $date->setTime(0, 0);
$date->add(new \DateInterval('P5D'));
+ $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return [
- [$date->format('Y-m-d'), true],
+ [$date->format('Y-m-d H:i:s'), true],
['abc', false],
- [$date->format('Y-m-d') . 'xyz', false],
+ [$date->format('Y-m-d H:i:s') . 'xyz', false],
];
}
$data = $result->getData();
$this->assertTrue(is_string($data['token']));
- $this->assertEquals($date, substr($data['expiration'], 0, 10));
+ $this->assertEquals(substr($date, 0, 10), substr($data['expiration'], 0, 10));
// check for correct link
$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
$share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
- $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d'));
+ $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d H:i:s'));
$this->shareManager->deleteShare($share);
}
$data = $result->getData();
$this->assertTrue(is_string($data['token']));
- $this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
+ $this->assertEquals($date->format('Y-m-d 00:00:00'), $data['expiration']);
// check for correct link
$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
use OCP\Files\Node;
use OCP\HintException;
use OCP\IConfig;
+use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
/** @var KnownUserService */
private $knownUserService;
private ShareDisableChecker $shareDisableChecker;
+ private IDateTimeZone $dateTimeZone;
public function __construct(
LoggerInterface $logger,
IEventDispatcher $dispatcher,
IUserSession $userSession,
KnownUserService $knownUserService,
- ShareDisableChecker $shareDisableChecker
+ ShareDisableChecker $shareDisableChecker,
+ IDateTimeZone $dateTimeZone,
) {
$this->logger = $logger;
$this->config = $config;
$this->userSession = $userSession;
$this->knownUserService = $knownUserService;
$this->shareDisableChecker = $shareDisableChecker;
+ $this->dateTimeZone = $dateTimeZone;
}
/**
$expirationDate = $share->getExpirationDate();
if ($expirationDate !== null) {
- //Make sure the expiration date is a date
+ $expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
if ($date >= $expirationDate) {
$message = $this->l->t('Expiration date is in the past');
$isEnforced = $this->shareApiInternalDefaultExpireDateEnforced();
}
if ($fullId === null && $expirationDate === null && $defaultExpireDate) {
- $expirationDate = new \DateTime();
+ $expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);
-
$days = (int)$this->config->getAppValue('core', $configProp, (string)$defaultExpireDays);
if ($days > $defaultExpireDays) {
$days = $defaultExpireDays;
throw new \InvalidArgumentException('Expiration date is enforced');
}
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
$date->add(new \DateInterval('P' . $defaultExpireDays . 'D'));
if ($date < $expirationDate) {
$expirationDate = $share->getExpirationDate();
if ($expirationDate !== null) {
- //Make sure the expiration date is a date
+ $expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
if ($date >= $expirationDate) {
$message = $this->l->t('Expiration date is in the past');
}
if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
- $expirationDate = new \DateTime();
+ $expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);
$days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', (string)$this->shareApiLinkDefaultExpireDays());
throw new \InvalidArgumentException('Expiration date is enforced');
}
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
if ($date < $expirationDate) {
throw new \Exception($message);
}
+ if ($expirationDate instanceof \DateTime) {
+ $expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
+ }
$share->setExpirationDate($expirationDate);
return $share;
namespace Test\Share20;
+use DateTimeZone;
use OC\Files\Mount\MoveableMount;
use OC\KnownUser\KnownUserService;
use OC\Share20\DefaultShareProvider;
use OCP\Files\Storage;
use OCP\HintException;
use OCP\IConfig;
+use OCP\IDateTimeZone;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
protected $knownUserService;
/** @var ShareDisableChecker|MockObject */
protected $shareDisabledChecker;
+ private DateTimeZone $timezone;
+ /** @var IDateTimeZone|MockObject */
+ protected $dateTimeZone;
protected function setUp(): void {
$this->logger = $this->createMock(LoggerInterface::class);
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->shareDisabledChecker = new ShareDisableChecker($this->config, $this->userManager, $this->groupManager);
+ $this->dateTimeZone = $this->createMock(IDateTimeZone::class);
+ $this->timezone = new \DateTimeZone('Pacific/Auckland');
+ $this->dateTimeZone->method('getTimeZone')->willReturnCallback(fn () => $this->timezone);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->l = $this->createMock(IL10N::class);
$this->dispatcher,
$this->userSession,
$this->knownUserService,
- $this->shareDisabledChecker
+ $this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$this->defaultProvider = $this->createMock(DefaultShareProvider::class);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
]);
}
]);
}
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
$expected->setTime(0, 0, 0);
$expected->add(new \DateInterval('P3D'));
]);
}
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
$expected->setTime(0, 0, 0);
$expected->add(new \DateInterval('P1D'));
* @dataProvider validateExpirationDateInternalProvider
*/
public function testValidateExpirationDateInternalEnforceValid($shareType) {
- $future = new \DateTime();
+ $future = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$future->add(new \DateInterval('P2D'));
$future->setTime(1, 2, 3);
* @dataProvider validateExpirationDateInternalProvider
*/
public function testValidateExpirationDateInternalNoDefault($shareType) {
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->add(new \DateInterval('P5D'));
$date->setTime(1, 2, 3);
$share = $this->manager->newShare();
$share->setShareType($shareType);
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
+ $expected->setTime(0, 0);
$expected->add(new \DateInterval('P3D'));
- $expected->setTime(0, 0, 0);
+ $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
if ($shareType === IShare::TYPE_USER) {
$this->config->method('getAppValue')
* @dataProvider validateExpirationDateInternalProvider
*/
public function testValidateExpirationDateInternalDefault($shareType) {
- $future = new \DateTime();
+ $future = new \DateTime('now', $this->timezone);
$future->add(new \DateInterval('P5D'));
$future->setTime(1, 2, 3);
$expected = clone $future;
- $expected->setTime(0, 0, 0);
+ $expected->setTime(0, 0);
$share = $this->manager->newShare();
$share->setShareType($shareType);
* @dataProvider validateExpirationDateInternalProvider
*/
public function testValidateExpirationDateInternalHookModification($shareType) {
- $nextWeek = new \DateTime();
+ $nextWeek = new \DateTime('now', $this->timezone);
$nextWeek->add(new \DateInterval('P7D'));
$nextWeek->setTime(0, 0, 0);
['core', 'link_defaultExpDays', '3', '3'],
]);
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
$expected->setTime(0, 0, 0);
$expected->add(new \DateInterval('P3D'));
['core', 'link_defaultExpDays', '3', '1'],
]);
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
$expected->setTime(0, 0, 0);
$expected->add(new \DateInterval('P1D'));
}
public function testValidateExpirationDateEnforceValid() {
- $future = new \DateTime();
+ $future = new \DateTime('now', $this->timezone);
$future->add(new \DateInterval('P2D'));
$future->setTime(1, 2, 3);
}
public function testValidateExpirationDateNoDefault() {
- $date = new \DateTime();
+ $date = new \DateTime('now', $this->timezone);
$date->add(new \DateInterval('P5D'));
$date->setTime(1, 2, 3);
$expected = clone $date;
- $expected->setTime(0, 0, 0);
+ $expected->setTime(0, 0);
+ $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$share = $this->manager->newShare();
$share->setExpirationDate($date);
public function testValidateExpirationDateNoDateDefault() {
$share = $this->manager->newShare();
- $expected = new \DateTime();
+ $expected = new \DateTime('now', $this->timezone);
$expected->add(new \DateInterval('P3D'));
- $expected->setTime(0, 0, 0);
+ $expected->setTime(0, 0);
+ $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$this->config->method('getAppValue')
->willReturnMap([
}
public function testValidateExpirationDateDefault() {
- $future = new \DateTime();
+ $future = new \DateTime('now', $this->timezone);
$future->add(new \DateInterval('P5D'));
$future->setTime(1, 2, 3);
$expected = clone $future;
- $expected->setTime(0, 0, 0);
+ $expected->setTime(0, 0);
+ $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
+
+ $share = $this->manager->newShare();
+ $share->setExpirationDate($future);
+
+ $this->config->method('getAppValue')
+ ->willReturnMap([
+ ['core', 'shareapi_default_expire_date', 'no', 'yes'],
+ ['core', 'shareapi_expire_after_n_days', '7', '3'],
+ ['core', 'link_defaultExpDays', '3', '1'],
+ ]);
+
+ $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
+ \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
+ $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
+ return $data['expirationDate'] == $expected;
+ }));
+
+ self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
+
+ $this->assertEquals($expected, $share->getExpirationDate());
+ }
+
+ public function testValidateExpirationNegativeOffsetTimezone() {
+ $this->timezone = new \DateTimeZone('Pacific/Tahiti');
+ $future = new \DateTime();
+ $future->add(new \DateInterval('P5D'));
+
+ $expected = clone $future;
+ $expected->setTimezone($this->timezone);
+ $expected->setTime(0, 0);
+ $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$share = $this->manager->newShare();
$share->setExpirationDate($future);
}
public function testValidateExpirationDateHookModification() {
- $nextWeek = new \DateTime();
+ $nextWeek = new \DateTime('now', $this->timezone);
$nextWeek->add(new \DateInterval('P7D'));
- $nextWeek->setTime(0, 0, 0);
$save = clone $nextWeek;
+ $save->setTime(0, 0);
+ $save->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
\OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$share = $this->createMock(IShare::class);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$share = $this->createMock(IShare::class);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$share = $this->createMock(IShare::class);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$this->assertSame($expected,
$manager->shareProviderExists($shareType)
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$factory->setProvider($this->defaultProvider);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$factory->setProvider($this->defaultProvider);
$this->userSession,
$this->knownUserService,
$this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$factory->setProvider($this->defaultProvider);
$this->dispatcher,
$this->userSession,
$this->knownUserService,
- $this->shareDisabledChecker
+ $this->shareDisabledChecker,
+ $this->dateTimeZone,
);
$factory->setProvider($this->defaultProvider);