changepropagator.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright (c) 2014 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\Cache;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Storage\Temporary;
  11. use OC\Files\View;
  12. class ChangePropagator extends \Test\TestCase {
  13. /**
  14. * @var \OC\Files\Cache\ChangePropagator
  15. */
  16. private $propagator;
  17. /**
  18. * @var \OC\Files\View
  19. */
  20. private $view;
  21. /**
  22. * @var \OC\Files\Storage\Storage
  23. */
  24. private $storage;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->storage = new Temporary(array());
  28. $root = $this->getUniqueID('/');
  29. Filesystem::mount($this->storage, array(), $root);
  30. $this->view = new View($root);
  31. $this->propagator = new \OC\Files\Cache\ChangePropagator($this->view);
  32. }
  33. public function testGetParentsSingle() {
  34. $this->propagator->addChange('/foo/bar/asd');
  35. $this->assertEquals(array('/', '/foo', '/foo/bar'), $this->propagator->getAllParents());
  36. }
  37. public function testGetParentsMultiple() {
  38. $this->propagator->addChange('/foo/bar/asd');
  39. $this->propagator->addChange('/foo/qwerty');
  40. $this->propagator->addChange('/foo/asd/bar');
  41. $this->assertEquals(array('/', '/foo', '/foo/bar', '/foo/asd'), $this->propagator->getAllParents());
  42. }
  43. public function testSinglePropagate() {
  44. $this->view->mkdir('/foo');
  45. $this->view->mkdir('/foo/bar');
  46. $this->view->file_put_contents('/foo/bar/sad.txt', 'qwerty');
  47. $oldInfo1 = $this->view->getFileInfo('/');
  48. $oldInfo2 = $this->view->getFileInfo('/foo');
  49. $oldInfo3 = $this->view->getFileInfo('/foo/bar');
  50. $time = time() + 50;
  51. $this->propagator->addChange('/foo/bar/sad.txt');
  52. $this->propagator->propagateChanges($time);
  53. $newInfo1 = $this->view->getFileInfo('/');
  54. $newInfo2 = $this->view->getFileInfo('/foo');
  55. $newInfo3 = $this->view->getFileInfo('/foo/bar');
  56. $this->assertEquals($newInfo1->getMTime(), $time);
  57. $this->assertEquals($newInfo2->getMTime(), $time);
  58. $this->assertEquals($newInfo3->getMTime(), $time);
  59. $this->assertNotSame($oldInfo1->getEtag(), $newInfo1->getEtag());
  60. $this->assertNotSame($oldInfo2->getEtag(), $newInfo2->getEtag());
  61. $this->assertNotSame($oldInfo3->getEtag(), $newInfo3->getEtag());
  62. }
  63. public function testDontLowerMtime() {
  64. $time = time();
  65. $this->view->mkdir('/foo');
  66. $this->view->mkdir('/foo/bar');
  67. $cache = $this->storage->getCache();
  68. $cache->put('', ['mtime' => $time - 50]);
  69. $cache->put('foo', ['mtime' => $time - 150]);
  70. $cache->put('foo/bar', ['mtime' => $time - 250]);
  71. $this->propagator->addChange('/foo/bar/foo');
  72. $this->propagator->propagateChanges($time - 100);
  73. $this->assertEquals(50, $time - $cache->get('')['mtime']);
  74. $this->assertEquals(100, $time - $cache->get('foo')['mtime']);
  75. $this->assertEquals(100, $time - $cache->get('foo/bar')['mtime']);
  76. }
  77. }