1
0

AppDirsWithDifferentOwnerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Tests;
  25. use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner;
  26. use OCP\IL10N;
  27. use Test\TestCase;
  28. class AppDirsWithDifferentOwnerTest extends TestCase {
  29. private IL10N $l10n;
  30. private AppDirsWithDifferentOwner $check;
  31. /**
  32. * Holds a list of directories created during tests.
  33. *
  34. * @var array
  35. */
  36. private $dirsToRemove = [];
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->l10n = $this->getMockBuilder(IL10N::class)
  40. ->disableOriginalConstructor()->getMock();
  41. $this->l10n->expects($this->any())
  42. ->method('t')
  43. ->willReturnCallback(function ($message, array $replace) {
  44. return vsprintf($message, $replace);
  45. });
  46. $this->check = new AppDirsWithDifferentOwner(
  47. $this->l10n,
  48. );
  49. }
  50. /**
  51. * Setups a temp directory and some subdirectories.
  52. * Then calls the 'getAppDirsWithDifferentOwner' method.
  53. * The result is expected to be empty since
  54. * there are no directories with different owners than the current user.
  55. *
  56. * @return void
  57. */
  58. public function testAppDirectoryOwnersOk() {
  59. $tempDir = tempnam(sys_get_temp_dir(), 'apps') . 'dir';
  60. mkdir($tempDir);
  61. mkdir($tempDir . DIRECTORY_SEPARATOR . 'app1');
  62. mkdir($tempDir . DIRECTORY_SEPARATOR . 'app2');
  63. $this->dirsToRemove[] = $tempDir . DIRECTORY_SEPARATOR . 'app1';
  64. $this->dirsToRemove[] = $tempDir . DIRECTORY_SEPARATOR . 'app2';
  65. $this->dirsToRemove[] = $tempDir;
  66. \OC::$APPSROOTS = [
  67. [
  68. 'path' => $tempDir,
  69. 'url' => '/apps',
  70. 'writable' => true,
  71. ],
  72. ];
  73. $this->assertSame(
  74. [],
  75. $this->invokePrivate($this->check, 'getAppDirsWithDifferentOwner', [posix_getuid()])
  76. );
  77. }
  78. /**
  79. * Calls the check for a none existing app root that is marked as not writable.
  80. * It's expected that no error happens since the check shouldn't apply.
  81. *
  82. * @return void
  83. */
  84. public function testAppDirectoryOwnersNotWritable() {
  85. $tempDir = tempnam(sys_get_temp_dir(), 'apps') . 'dir';
  86. \OC::$APPSROOTS = [
  87. [
  88. 'path' => $tempDir,
  89. 'url' => '/apps',
  90. 'writable' => false,
  91. ],
  92. ];
  93. $this->assertSame(
  94. [],
  95. $this->invokePrivate($this->check, 'getAppDirsWithDifferentOwner', [posix_getuid()])
  96. );
  97. }
  98. /**
  99. * Removes directories created during tests.
  100. *
  101. * @after
  102. * @return void
  103. */
  104. public function removeTestDirectories() {
  105. foreach ($this->dirsToRemove as $dirToRemove) {
  106. rmdir($dirToRemove);
  107. }
  108. $this->dirsToRemove = [];
  109. }
  110. }