SetPasswordColumnTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests\Migration;
  26. use OCA\Files_Sharing\Migration\SetPasswordColumn;
  27. use OCA\Files_Sharing\Tests\TestCase;
  28. use OCP\IConfig;
  29. use OCP\Migration\IOutput;
  30. use OCP\Share\IShare;
  31. /**
  32. * Class SetPasswordColumnTest
  33. *
  34. * @group DB
  35. */
  36. class SetPasswordColumnTest extends TestCase {
  37. /** @var \OCP\IDBConnection */
  38. private $connection;
  39. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  40. private $config;
  41. /** @var SetPasswordColumn */
  42. private $migration;
  43. private $table = 'share';
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->connection = \OC::$server->getDatabaseConnection();
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->migration = new SetPasswordColumn($this->connection, $this->config);
  49. $this->cleanDB();
  50. }
  51. protected function tearDown(): void {
  52. parent::tearDown();
  53. $this->cleanDB();
  54. }
  55. private function cleanDB() {
  56. $query = $this->connection->getQueryBuilder();
  57. $query->delete($this->table)->execute();
  58. }
  59. public function testAddPasswordColumn() {
  60. $this->config->expects($this->once())
  61. ->method('getAppValue')
  62. ->with('files_sharing', 'installed_version', '0.0.0')
  63. ->willReturn('1.3.0');
  64. $shareTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL, IShare::TYPE_LINK];
  65. foreach ($shareTypes as $shareType) {
  66. for ($i = 0; $i < 5; $i++) {
  67. $query = $this->connection->getQueryBuilder();
  68. $query->insert($this->table)
  69. ->values([
  70. 'share_type' => $query->createNamedParameter($shareType),
  71. 'share_with' => $query->createNamedParameter('shareWith'),
  72. 'uid_owner' => $query->createNamedParameter('user' . $i),
  73. 'uid_initiator' => $query->createNamedParameter(null),
  74. 'parent' => $query->createNamedParameter(0),
  75. 'item_type' => $query->createNamedParameter('file'),
  76. 'item_source' => $query->createNamedParameter('2'),
  77. 'item_target' => $query->createNamedParameter('/2'),
  78. 'file_source' => $query->createNamedParameter(2),
  79. 'file_target' => $query->createNamedParameter('/foobar'),
  80. 'permissions' => $query->createNamedParameter(31),
  81. 'stime' => $query->createNamedParameter(time()),
  82. ]);
  83. $this->assertSame(1, $query->execute());
  84. }
  85. }
  86. /** @var IOutput $output */
  87. $output = $this->createMock(IOutput::class);
  88. $this->migration->run($output);
  89. $query = $this->connection->getQueryBuilder();
  90. $query->select('*')
  91. ->from('share');
  92. $result = $query->execute();
  93. $allShares = $result->fetchAll();
  94. $result->closeCursor();
  95. foreach ($allShares as $share) {
  96. if ((int)$share['share_type'] === IShare::TYPE_LINK) {
  97. $this->assertNull($share['share_with']);
  98. $this->assertSame('shareWith', $share['password']);
  99. } else {
  100. $this->assertSame('shareWith', $share['share_with']);
  101. $this->assertNull($share['password']);
  102. }
  103. }
  104. }
  105. }