Skip to content

Commit

Permalink
Fix sharing creation insert and get
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv committed Sep 2, 2023
1 parent 12f57ad commit 3087dcd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ public function testDeleteShare() {
->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
->setShareType(IShare::TYPE_LINK)
->setPermissions(1);
$share2 = $this->shareManager->createShare($share1);
$share2 = $this->shareManager->createShare($share2);

$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
$ocs->deleteShare($share1->getId());
Expand Down
54 changes: 22 additions & 32 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
use OC\Share20\Exception\BackendError;
use OC\Share20\Exception\InvalidShare;
use OC\Share20\Exception\ProviderException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -90,19 +90,19 @@ class DefaultShareProvider implements IShareProvider {
/** @var IURLGenerator */
private $urlGenerator;

/** @var IConfig */
private $config;
private ITimeFactory $timeFactory;

public function __construct(
IDBConnection $connection,
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IMailer $mailer,
Defaults $defaults,
IFactory $l10nFactory,
IURLGenerator $urlGenerator,
IConfig $config) {
IDBConnection $connection,
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IMailer $mailer,
Defaults $defaults,
IFactory $l10nFactory,
IURLGenerator $urlGenerator,
ITimeFactory $timeFactory,
) {
$this->dbConn = $connection;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
Expand All @@ -111,7 +111,7 @@ public function __construct(
$this->defaults = $defaults;
$this->l10nFactory = $l10nFactory;
$this->urlGenerator = $urlGenerator;
$this->config = $config;
$this->timeFactory = $timeFactory;
}

/**
Expand Down Expand Up @@ -216,32 +216,22 @@ public function create(\OCP\Share\IShare $share) {
}

// Set the time this share was created
$qb->setValue('stime', $qb->createNamedParameter(time()));
$shareTime = $this->timeFactory->now();
$qb->setValue('stime', $qb->createNamedParameter($shareTime->getTimestamp()));

// insert the data and fetch the id of the share
$this->dbConn->beginTransaction();
$qb->execute();
$id = $this->dbConn->lastInsertId('*PREFIX*share');

// Now fetch the inserted share and create a complete share object
$qb = $this->dbConn->getQueryBuilder();
$qb->select('*')
->from('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
$qb->executeStatement();

$cursor = $qb->execute();
$data = $cursor->fetch();
$this->dbConn->commit();
$cursor->closeCursor();
// Update mandatory data
$id = $qb->getLastInsertId();
$share->setId((string)$id);
$share->setProviderId($this->identifier());

if ($data === false) {
throw new ShareNotFound('Newly created share could not be found');
}
$share->setShareTime(\DateTime::createFromImmutable($shareTime));

$mailSendValue = $share->getMailSend();
$data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
$share->setMailSend(($mailSendValue === null) ? true : $mailSendValue);

$share = $this->createShare($data);
return $share;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCA\ShareByMail\Settings\SettingsManager;
use OCA\ShareByMail\ShareByMailProvider;
use OCA\Talk\Share\RoomShareProvider;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IServerContainer;
Expand Down Expand Up @@ -104,7 +105,7 @@ protected function defaultShareProvider() {
$this->serverContainer->query(Defaults::class),
$this->serverContainer->getL10NFactory(),
$this->serverContainer->getURLGenerator(),
$this->serverContainer->getConfig()
$this->serverContainer->query(ITimeFactory::class),
);
}

Expand Down
21 changes: 11 additions & 10 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

use OC\Share20\DefaultShareProvider;
use OC\Share20\ShareAttributes;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -79,8 +79,8 @@ class DefaultShareProviderTest extends \Test\TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
protected $urlGenerator;

/** @var IConfig|MockObject */
protected $config;
/** @var ITimeFactory|MockObject */
protected $timeFactory;

protected function setUp(): void {
$this->dbConn = \OC::$server->getDatabaseConnection();
Expand All @@ -92,9 +92,10 @@ protected function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);

$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
$this->timeFactory->expects($this->any())->method('now')->willReturn(new \DateTimeImmutable("2023-05-04 00:00 Europe/Berlin"));

//Empty share table
$this->dbConn->getQueryBuilder()->delete('share')->execute();
Expand All @@ -108,7 +109,7 @@ protected function setUp(): void {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
);
}

Expand Down Expand Up @@ -469,7 +470,7 @@ public function testDeleteSingleShare() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
])
->setMethods(['getShareById'])
->getMock();
Expand Down Expand Up @@ -564,7 +565,7 @@ public function testDeleteGroupShareWithUserGroupShares() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
])
->setMethods(['getShareById'])
->getMock();
Expand Down Expand Up @@ -2524,7 +2525,7 @@ public function testGetSharesInFolder() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
);

$password = md5(time());
Expand Down Expand Up @@ -2622,7 +2623,7 @@ public function testGetAccessListNoCurrentAccessRequired() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
);

$u1 = $userManager->createUser('testShare1', 'test');
Expand Down Expand Up @@ -2718,7 +2719,7 @@ public function testGetAccessListCurrentAccessRequired() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->timeFactory
);

$u1 = $userManager->createUser('testShare1', 'test');
Expand Down

0 comments on commit 3087dcd

Please sign in to comment.