amazons3migration.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace Test\Files\Storage;
  25. class AmazonS3Migration extends \Test\TestCase {
  26. /**
  27. * @var \OC\Files\Storage\Storage instance
  28. */
  29. protected $instance;
  30. /** @var array */
  31. protected $params;
  32. /** @var string */
  33. protected $oldId;
  34. /** @var string */
  35. protected $newId;
  36. protected function setUp() {
  37. parent::setUp();
  38. $uuid = $this->getUniqueID();
  39. $this->params['key'] = 'key'.$uuid;
  40. $this->params['secret'] = 'secret'.$uuid;
  41. $this->params['bucket'] = 'bucket'.$uuid;
  42. $this->oldId = 'amazon::' . $this->params['key'] . md5($this->params['secret']);
  43. $this->newId = 'amazon::' . $this->params['bucket'];
  44. }
  45. protected function tearDown() {
  46. $this->deleteStorage($this->oldId);
  47. $this->deleteStorage($this->newId);
  48. parent::tearDown();
  49. }
  50. public function testUpdateLegacyOnlyId () {
  51. // add storage ids
  52. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  53. // add file to old cache
  54. $fileId = $oldCache->put('foobar', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  55. try {
  56. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  57. } catch (\Exception $e) {
  58. //ignore
  59. }
  60. $storages = $this->getStorages();
  61. $this->assertTrue(isset($storages[$this->newId]));
  62. $this->assertFalse(isset($storages[$this->oldId]));
  63. $this->assertSame((int)$oldCache->getNumericStorageId(), (int)$storages[$this->newId]);
  64. list($storageId, $path) = \OC\Files\Cache\Cache::getById($fileId);
  65. $this->assertSame($this->newId, $storageId);
  66. $this->assertSame('foobar', $path);
  67. }
  68. public function testUpdateLegacyAndNewId () {
  69. // add storage ids
  70. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  71. new \OC\Files\Cache\Cache($this->newId);
  72. // add file to old cache
  73. $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  74. try {
  75. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  76. } catch (\Exception $e) {
  77. //ignore
  78. }
  79. $storages = $this->getStorages();
  80. $this->assertTrue(isset($storages[$this->newId]));
  81. $this->assertFalse(isset($storages[$this->oldId]));
  82. $this->assertNull(\OC\Files\Cache\Cache::getById($fileId), 'old filecache has not been cleared');
  83. }
  84. /**
  85. * @param $storages
  86. * @return array
  87. */
  88. public function getStorages() {
  89. $storages = array();
  90. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  91. 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)'
  92. );
  93. $stmt->execute(array($this->oldId, $this->newId));
  94. while ($row = $stmt->fetch()) {
  95. $storages[$row['id']] = $row['numeric_id'];
  96. }
  97. return $storages;
  98. }
  99. public function deleteStorage($id) {
  100. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  101. 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'
  102. );
  103. $stmt->execute(array($id));
  104. }
  105. }