SetPasswordColumnTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\Migration;
  8. use OCA\Files_Sharing\Migration\SetPasswordColumn;
  9. use OCA\Files_Sharing\Tests\TestCase;
  10. use OCP\IConfig;
  11. use OCP\Migration\IOutput;
  12. use OCP\Share\IShare;
  13. /**
  14. * Class SetPasswordColumnTest
  15. *
  16. * @group DB
  17. */
  18. class SetPasswordColumnTest extends TestCase {
  19. /** @var \OCP\IDBConnection */
  20. private $connection;
  21. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  22. private $config;
  23. /** @var SetPasswordColumn */
  24. private $migration;
  25. private $table = 'share';
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->connection = \OC::$server->getDatabaseConnection();
  29. $this->config = $this->createMock(IConfig::class);
  30. $this->migration = new SetPasswordColumn($this->connection, $this->config);
  31. $this->cleanDB();
  32. }
  33. protected function tearDown(): void {
  34. parent::tearDown();
  35. $this->cleanDB();
  36. }
  37. private function cleanDB() {
  38. $query = $this->connection->getQueryBuilder();
  39. $query->delete($this->table)->execute();
  40. }
  41. public function testAddPasswordColumn() {
  42. $this->config->expects($this->once())
  43. ->method('getAppValue')
  44. ->with('files_sharing', 'installed_version', '0.0.0')
  45. ->willReturn('1.3.0');
  46. $shareTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL, IShare::TYPE_LINK];
  47. foreach ($shareTypes as $shareType) {
  48. for ($i = 0; $i < 5; $i++) {
  49. $query = $this->connection->getQueryBuilder();
  50. $query->insert($this->table)
  51. ->values([
  52. 'share_type' => $query->createNamedParameter($shareType),
  53. 'share_with' => $query->createNamedParameter('shareWith'),
  54. 'uid_owner' => $query->createNamedParameter('user' . $i),
  55. 'uid_initiator' => $query->createNamedParameter(null),
  56. 'parent' => $query->createNamedParameter(0),
  57. 'item_type' => $query->createNamedParameter('file'),
  58. 'item_source' => $query->createNamedParameter('2'),
  59. 'item_target' => $query->createNamedParameter('/2'),
  60. 'file_source' => $query->createNamedParameter(2),
  61. 'file_target' => $query->createNamedParameter('/foobar'),
  62. 'permissions' => $query->createNamedParameter(31),
  63. 'stime' => $query->createNamedParameter(time()),
  64. ]);
  65. $this->assertSame(1, $query->execute());
  66. }
  67. }
  68. /** @var IOutput $output */
  69. $output = $this->createMock(IOutput::class);
  70. $this->migration->run($output);
  71. $query = $this->connection->getQueryBuilder();
  72. $query->select('*')
  73. ->from('share');
  74. $result = $query->execute();
  75. $allShares = $result->fetchAll();
  76. $result->closeCursor();
  77. foreach ($allShares as $share) {
  78. if ((int)$share['share_type'] === IShare::TYPE_LINK) {
  79. $this->assertNull($share['share_with']);
  80. $this->assertSame('shareWith', $share['password']);
  81. } else {
  82. $this->assertSame('shareWith', $share['share_with']);
  83. $this->assertNull($share['password']);
  84. }
  85. }
  86. }
  87. }