scanner.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Utils;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Mount\MountPoint;
  11. use OC\Files\Storage\Temporary;
  12. class TestScanner extends \OC\Files\Utils\Scanner {
  13. /**
  14. * @var \OC\Files\Mount\MountPoint[] $mounts
  15. */
  16. private $mounts = array();
  17. /**
  18. * @param \OC\Files\Mount\MountPoint $mount
  19. */
  20. public function addMount($mount) {
  21. $this->mounts[] = $mount;
  22. }
  23. protected function getMounts($dir) {
  24. return $this->mounts;
  25. }
  26. public function getPropagator() {
  27. return $this->propagator;
  28. }
  29. public function setPropagator($propagator) {
  30. $this->propagator = $propagator;
  31. }
  32. }
  33. class Scanner extends \Test\TestCase {
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->loginAsUser();
  37. }
  38. protected function tearDown() {
  39. $this->logout();
  40. parent::tearDown();
  41. }
  42. public function testReuseExistingRoot() {
  43. $storage = new Temporary(array());
  44. $mount = new MountPoint($storage, '');
  45. Filesystem::getMountManager()->addMount($mount);
  46. $cache = $storage->getCache();
  47. $storage->mkdir('folder');
  48. $storage->file_put_contents('foo.txt', 'qwerty');
  49. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  50. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
  51. $scanner->addMount($mount);
  52. $scanner->scan('');
  53. $this->assertTrue($cache->inCache('folder/bar.txt'));
  54. $oldRoot = $cache->get('');
  55. $scanner->scan('');
  56. $newRoot = $cache->get('');
  57. $this->assertEquals($oldRoot, $newRoot);
  58. }
  59. public function testReuseExistingFile() {
  60. $storage = new Temporary(array());
  61. $mount = new MountPoint($storage, '');
  62. Filesystem::getMountManager()->addMount($mount);
  63. $cache = $storage->getCache();
  64. $storage->mkdir('folder');
  65. $storage->file_put_contents('foo.txt', 'qwerty');
  66. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  67. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
  68. $scanner->addMount($mount);
  69. $scanner->scan('');
  70. $this->assertTrue($cache->inCache('folder/bar.txt'));
  71. $old = $cache->get('folder/bar.txt');
  72. $scanner->scan('');
  73. $new = $cache->get('folder/bar.txt');
  74. $this->assertEquals($old, $new);
  75. }
  76. public function testChangePropagator() {
  77. /**
  78. * @var \OC\Files\Cache\ChangePropagator $propagator
  79. */
  80. $propagator = $this->getMock('\OC\Files\Cache\ChangePropagator', array('propagateChanges'), array(), '', false);
  81. $storage = new Temporary(array());
  82. $mount = new MountPoint($storage, '/foo');
  83. Filesystem::getMountManager()->addMount($mount);
  84. $cache = $storage->getCache();
  85. $storage->mkdir('folder');
  86. $storage->file_put_contents('foo.txt', 'qwerty');
  87. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  88. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection());
  89. $originalPropagator = $scanner->getPropagator();
  90. $scanner->setPropagator($propagator);
  91. $scanner->addMount($mount);
  92. $scanner->scan('');
  93. $changes = $propagator->getChanges();
  94. $parents = $propagator->getAllParents();
  95. sort($changes);
  96. sort($parents);
  97. $this->assertEquals(array('/foo', '/foo/folder', '/foo/folder/bar.txt', '/foo/foo.txt'), $changes);
  98. $this->assertEquals(array('/', '/foo', '/foo/folder'), $parents);
  99. $cache->put('foo.txt', array('storage_mtime' => time() - 50));
  100. $propagator = $this->getMock('\OC\Files\Cache\ChangePropagator', array('propagateChanges'), array(), '', false);
  101. $scanner->setPropagator($propagator);
  102. $storage->file_put_contents('foo.txt', 'asdasd');
  103. $scanner->scan('');
  104. $changes = $propagator->getChanges();
  105. $parents = $propagator->getAllParents();
  106. $this->assertEquals(array('/foo/foo.txt'), $changes);
  107. $this->assertEquals(array('/', '/foo'), $parents);
  108. $scanner->setPropagator($originalPropagator);
  109. $oldInfo = $cache->get('');
  110. $cache->put('foo.txt', array('storage_mtime' => time() - 70));
  111. $storage->file_put_contents('foo.txt', 'asdasd');
  112. $scanner->scan('');
  113. $newInfo = $cache->get('');
  114. $this->assertNotEquals($oldInfo['etag'], $newInfo['etag']);
  115. }
  116. }