SetPasswordColumnTest.php 3.5 KB

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