1
0

amazons3migration.php 3.9 KB

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