amazons3migration.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 \PHPUnit_Framework_TestCase {
  23. /**
  24. * @var \OC\Files\Storage\Storage instance
  25. */
  26. protected $instance;
  27. public function setUp () {
  28. $uuid = uniqid();
  29. $this->params['key'] = 'key'.$uuid;
  30. $this->params['secret'] = 'secret'.$uuid;
  31. $this->params['bucket'] = 'bucket'.$uuid;
  32. $this->oldId = 'amazon::' . $this->params['key'] . md5($this->params['secret']);
  33. $this->newId = 'amazon::' . $this->params['bucket'];
  34. }
  35. public function tearDown () {
  36. $this->deleteStorage($this->oldId);
  37. $this->deleteStorage($this->newId);
  38. }
  39. public function testUpdateLegacyOnlyId () {
  40. // add storage ids
  41. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  42. // add file to old cache
  43. $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  44. try {
  45. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  46. } catch (\Exception $e) {
  47. //ignore
  48. }
  49. $storages = $this->getStorages();
  50. $this->assertTrue(isset($storages[$this->newId]));
  51. $this->assertFalse(isset($storages[$this->oldId]));
  52. $this->assertSame((int)$oldCache->getNumericStorageId(), (int)$storages[$this->newId]);
  53. list($storageId, $path) = \OC\Files\Cache\Cache::getById($fileId);
  54. $this->assertSame($this->newId, $storageId);
  55. $this->assertSame('/', $path);
  56. }
  57. public function testUpdateLegacyAndNewId () {
  58. // add storage ids
  59. $oldCache = new \OC\Files\Cache\Cache($this->oldId);
  60. new \OC\Files\Cache\Cache($this->newId);
  61. // add file to old cache
  62. $fileId = $oldCache->put('/', array('size' => 0, 'mtime' => time(), 'mimetype' => 'httpd/directory'));
  63. try {
  64. $this->instance = new \OC\Files\Storage\AmazonS3($this->params);
  65. } catch (\Exception $e) {
  66. //ignore
  67. }
  68. $storages = $this->getStorages();
  69. $this->assertTrue(isset($storages[$this->newId]));
  70. $this->assertFalse(isset($storages[$this->oldId]));
  71. $this->assertNull(\OC\Files\Cache\Cache::getById($fileId), 'old filecache has not been cleared');
  72. }
  73. /**
  74. * @param $storages
  75. * @return array
  76. */
  77. public function getStorages() {
  78. $storages = array();
  79. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  80. 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)'
  81. );
  82. $stmt->execute(array($this->oldId, $this->newId));
  83. while ($row = $stmt->fetch()) {
  84. $storages[$row['id']] = $row['numeric_id'];
  85. }
  86. return $storages;
  87. }
  88. public function deleteStorage($id) {
  89. $stmt = \OC::$server->getDatabaseConnection()->prepare(
  90. 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'
  91. );
  92. $stmt->execute(array($id));
  93. }
  94. }