amazons3migration.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jörn Friedrich Dreyer
  6. * @copyright 2012 Jörn Friedrich Dreyer jfd@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library 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
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace Test\Files\Storage;
  22. class AmazonS3Migration extends \Test\TestCase {
  23. /**
  24. * @var \OC\Files\Storage\Storage instance
  25. */
  26. protected $instance;
  27. /** @var array */
  28. protected $params;
  29. /** @var string */
  30. protected $oldId;
  31. /** @var string */
  32. protected $newId;
  33. protected function setUp() {
  34. parent::setUp();
  35. $uuid = $this->getUniqueID();
  36. $this->params['key'] = 'key'.$uuid;
  37. $this->params['secret'] = 'secret'.$uuid;
  38. $this->params['bucket'] = 'bucket'.$uuid;
  39. $this->oldId = 'amazon::' . $this->params['key'] . md5($this->params['secret']);
  40. $this->newId = 'amazon::' . $this->params['bucket'];
  41. }
  42. protected function tearDown() {
  43. $this->deleteStorage($this->oldId);
  44. $this->deleteStorage($this->newId);
  45. parent::tearDown();
  46. }
  47. public function testUpdateLegacyOnlyId () {
  48. // add storage ids
  49. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  50. // add file to old cache
  51. $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  52. try {
  53. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  54. } catch (\Exception $e) {
  55. //ignore
  56. }
  57. $storages = $this->getStorages();
  58. $this->assertTrue(isset($storages[$this->newId]));
  59. $this->assertFalse(isset($storages[$this->oldId]));
  60. $this->assertSame((int)$oldCache->getNumericStorageId(), (int)$storages[$this->newId]);
  61. list($storageId, $path) = \OC\Files\Cache\Cache::getById($fileId);
  62. $this->assertSame($this->newId, $storageId);
  63. $this->assertSame('/', $path);
  64. }
  65. public function testUpdateLegacyAndNewId () {
  66. // add storage ids
  67. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  68. new \OC\Files\Cache\Cache($this->newId);
  69. // add file to old cache
  70. $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  71. try {
  72. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  73. } catch (\Exception $e) {
  74. //ignore
  75. }
  76. $storages = $this->getStorages();
  77. $this->assertTrue(isset($storages[$this->newId]));
  78. $this->assertFalse(isset($storages[$this->oldId]));
  79. $this->assertNull(\OC\Files\Cache\Cache::getById($fileId), 'old filecache has not been cleared');
  80. }
  81. /**
  82. * @param $storages
  83. * @return array
  84. */
  85. public function getStorages() {
  86. $storages = array();
  87. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  88. 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)'
  89. );
  90. $stmt->execute(array($this->oldId, $this->newId));
  91. while ($row = $stmt->fetch()) {
  92. $storages[$row['id']] = $row['numeric_id'];
  93. }
  94. return $storages;
  95. }
  96. public function deleteStorage($id) {
  97. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  98. 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'
  99. );
  100. $stmt->execute(array($id));
  101. }
  102. }