CachePermissionsMaskTest.php 2.8 KB

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