1
0

CachePermissionsMaskTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Wrapper;
  9. use OCP\Constants;
  10. use Test\Files\Cache\CacheTest;
  11. /**
  12. * Class CachePermissionsMask
  13. *
  14. * @group DB
  15. *
  16. * @package Test\Files\Cache\Wrapper
  17. */
  18. class CachePermissionsMaskTest extends CacheTest {
  19. /**
  20. * @var \OC\Files\Cache\Cache $sourceCache
  21. */
  22. protected $sourceCache;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->storage->mkdir('foo');
  26. $this->sourceCache = $this->cache;
  27. $this->cache = $this->getMaskedCached(Constants::PERMISSION_ALL);
  28. }
  29. protected function getMaskedCached($mask) {
  30. return new \OC\Files\Cache\Wrapper\CachePermissionsMask($this->sourceCache, $mask);
  31. }
  32. public function maskProvider() {
  33. return [
  34. [Constants::PERMISSION_ALL],
  35. [Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE],
  36. [Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE],
  37. [Constants::PERMISSION_READ]
  38. ];
  39. }
  40. /**
  41. * @dataProvider maskProvider
  42. * @param int $mask
  43. */
  44. public function testGetMasked($mask) {
  45. $cache = $this->getMaskedCached($mask);
  46. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL];
  47. $this->sourceCache->put('foo', $data);
  48. $result = $cache->get('foo');
  49. $this->assertEquals($mask, $result['permissions']);
  50. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE];
  51. $this->sourceCache->put('bar', $data);
  52. $result = $cache->get('bar');
  53. $this->assertEquals($mask & ~Constants::PERMISSION_DELETE, $result['permissions']);
  54. }
  55. /**
  56. * @dataProvider maskProvider
  57. * @param int $mask
  58. */
  59. public function testGetFolderContentMasked($mask) {
  60. $this->storage->mkdir('foo');
  61. $this->storage->file_put_contents('foo/bar', 'asd');
  62. $this->storage->file_put_contents('foo/asd', 'bar');
  63. $this->storage->getScanner()->scan('');
  64. $cache = $this->getMaskedCached($mask);
  65. $files = $cache->getFolderContents('foo');
  66. $this->assertCount(2, $files);
  67. foreach ($files as $file) {
  68. $this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']);
  69. }
  70. }
  71. /**
  72. * @dataProvider maskProvider
  73. * @param int $mask
  74. */
  75. public function testSearchMasked($mask) {
  76. $this->storage->mkdir('foo');
  77. $this->storage->file_put_contents('foo/bar', 'asd');
  78. $this->storage->file_put_contents('foo/foobar', 'bar');
  79. $this->storage->getScanner()->scan('');
  80. $cache = $this->getMaskedCached($mask);
  81. $files = $cache->search('%bar');
  82. $this->assertCount(2, $files);
  83. foreach ($files as $file) {
  84. $this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']);
  85. }
  86. }
  87. }