AppDirsWithDifferentOwnerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests;
  8. use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner;
  9. use OCP\IL10N;
  10. use Test\TestCase;
  11. class AppDirsWithDifferentOwnerTest extends TestCase {
  12. private IL10N $l10n;
  13. private AppDirsWithDifferentOwner $check;
  14. /**
  15. * Holds a list of directories created during tests.
  16. *
  17. * @var array
  18. */
  19. private $dirsToRemove = [];
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->l10n = $this->getMockBuilder(IL10N::class)
  23. ->disableOriginalConstructor()->getMock();
  24. $this->l10n->expects($this->any())
  25. ->method('t')
  26. ->willReturnCallback(function ($message, array $replace) {
  27. return vsprintf($message, $replace);
  28. });
  29. $this->check = new AppDirsWithDifferentOwner(
  30. $this->l10n,
  31. );
  32. }
  33. /**
  34. * Setups a temp directory and some subdirectories.
  35. * Then calls the 'getAppDirsWithDifferentOwner' method.
  36. * The result is expected to be empty since
  37. * there are no directories with different owners than the current user.
  38. *
  39. * @return void
  40. */
  41. public function testAppDirectoryOwnersOk() {
  42. $tempDir = tempnam(sys_get_temp_dir(), 'apps') . 'dir';
  43. mkdir($tempDir);
  44. mkdir($tempDir . DIRECTORY_SEPARATOR . 'app1');
  45. mkdir($tempDir . DIRECTORY_SEPARATOR . 'app2');
  46. $this->dirsToRemove[] = $tempDir . DIRECTORY_SEPARATOR . 'app1';
  47. $this->dirsToRemove[] = $tempDir . DIRECTORY_SEPARATOR . 'app2';
  48. $this->dirsToRemove[] = $tempDir;
  49. \OC::$APPSROOTS = [
  50. [
  51. 'path' => $tempDir,
  52. 'url' => '/apps',
  53. 'writable' => true,
  54. ],
  55. ];
  56. $this->assertSame(
  57. [],
  58. $this->invokePrivate($this->check, 'getAppDirsWithDifferentOwner', [posix_getuid()])
  59. );
  60. }
  61. /**
  62. * Calls the check for a none existing app root that is marked as not writable.
  63. * It's expected that no error happens since the check shouldn't apply.
  64. *
  65. * @return void
  66. */
  67. public function testAppDirectoryOwnersNotWritable() {
  68. $tempDir = tempnam(sys_get_temp_dir(), 'apps') . 'dir';
  69. \OC::$APPSROOTS = [
  70. [
  71. 'path' => $tempDir,
  72. 'url' => '/apps',
  73. 'writable' => false,
  74. ],
  75. ];
  76. $this->assertSame(
  77. [],
  78. $this->invokePrivate($this->check, 'getAppDirsWithDifferentOwner', [posix_getuid()])
  79. );
  80. }
  81. /**
  82. * Removes directories created during tests.
  83. *
  84. * @after
  85. * @return void
  86. */
  87. public function removeTestDirectories() {
  88. foreach ($this->dirsToRemove as $dirToRemove) {
  89. rmdir($dirToRemove);
  90. }
  91. $this->dirsToRemove = [];
  92. }
  93. }