1
0

FixMountStoragesTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Repair\NC11;
  24. use OC\Repair\NC11\FixMountStorages;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\Migration\IOutput;
  28. use Test\TestCase;
  29. /**
  30. * Class FixMountStoragesTest
  31. *
  32. * @package Test\Repair\NC11
  33. * @group DB
  34. */
  35. class FixMountStoragesTest extends TestCase {
  36. /** @var IDBConnection */
  37. private $db;
  38. /** @var FixMountStorages */
  39. private $repair;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->db = \OC::$server->getDatabaseConnection();
  43. $this->repair = new FixMountStorages(
  44. $this->db
  45. );
  46. }
  47. public function testGetName() {
  48. $this->assertSame('Fix potential broken mount points', $this->repair->getName());
  49. }
  50. public function testRun() {
  51. // Valid mount
  52. $file1 = $this->createFile(42);
  53. $mount1 = $this->createMount($file1, 42);
  54. $this->assertStorage($mount1, 42);
  55. // Broken mount
  56. $file2 = $this->createFile(23);
  57. $mount2 = $this->createMount($file2, 1337);
  58. $this->assertStorage($mount2, 1337);
  59. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $output */
  60. $output = $this->createMock(IOutput::class);
  61. $output->expects($this->at(0))
  62. ->method('info')
  63. ->with('1 mounts updated');
  64. $this->repair->run($output);
  65. $this->assertStorage($mount1, 42);
  66. $this->assertStorage($mount2, 23);
  67. $output->expects($this->at(0))
  68. ->method('info')
  69. ->with('No mounts updated');
  70. $this->repair->run($output);
  71. $this->assertStorage($mount1, 42);
  72. $this->assertStorage($mount2, 23);
  73. }
  74. protected function createFile($storage) {
  75. $query = $this->db->getQueryBuilder();
  76. $query->insert('filecache')
  77. ->values([
  78. 'storage' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT),
  79. 'path_hash' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  80. 'encrypted' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  81. 'size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  82. 'unencrypted_size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  83. ]);
  84. $query->execute();
  85. return $query->getLastInsertId();
  86. }
  87. protected function createMount($fileId, $storage) {
  88. $query = $this->db->getQueryBuilder();
  89. $query->insert('mounts')
  90. ->values([
  91. 'storage_id' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT),
  92. 'root_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  93. 'user_id' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  94. 'mount_point' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  95. ]);
  96. $query->execute();
  97. return $query->getLastInsertId();
  98. }
  99. protected function assertStorage($mount, $storage) {
  100. $query = $this->db->getQueryBuilder();
  101. $query->select('storage_id')
  102. ->from('mounts')
  103. ->where($query->expr()->eq('id', $query->createNamedParameter($mount, IQueryBuilder::PARAM_INT)));
  104. $result = $query->execute();
  105. $row = $result->fetch();
  106. $result->closeCursor();
  107. $this->assertEquals($storage, $row['storage_id']);
  108. }
  109. }