SetPasswordColumnTest.php 3.0 KB

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