1616use CodeIgniter \Test \CIUnitTestCase ;
1717use CodeIgniter \Test \StreamFilterTrait ;
1818use PHPUnit \Framework \Attributes \Group ;
19+ use PHPUnit \Framework \Attributes \RequiresOperatingSystem ;
1920
2021/**
2122 * @internal
@@ -31,10 +32,22 @@ protected function setUp(): void
3132 {
3233 parent ::setUp ();
3334
35+ command ('debugbar:clear ' );
36+ $ this ->resetStreamFilterBuffer ();
37+
3438 $ this ->time = time ();
39+ $ this ->createDummyDebugbarJson ();
40+ }
41+
42+ protected function tearDown (): void
43+ {
44+ command ('debugbar:clear ' );
45+ $ this ->resetStreamFilterBuffer ();
46+
47+ parent ::tearDown ();
3548 }
3649
37- protected function createDummyDebugbarJson (): void
50+ private function createDummyDebugbarJson (): void
3851 {
3952 $ time = $ this ->time ;
4053 $ path = WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . "debugbar_ {$ time }.json " ;
@@ -50,18 +63,56 @@ protected function createDummyDebugbarJson(): void
5063
5164 public function testClearDebugbarWorks (): void
5265 {
53- // test clean debugbar dir
54- $ this ->assertFileDoesNotExist (WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . "debugbar_ {$ this ->time }.json " );
55-
56- // test dir is now populated with json
57- $ this ->createDummyDebugbarJson ();
5866 $ this ->assertFileExists (WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . "debugbar_ {$ this ->time }.json " );
5967
6068 command ('debugbar:clear ' );
61- $ result = $ this ->getStreamFilterBuffer ();
6269
6370 $ this ->assertFileDoesNotExist (WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . "debugbar_ {$ this ->time }.json " );
6471 $ this ->assertFileExists (WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . 'index.html ' );
65- $ this ->assertStringContainsString ('Debugbar cleared. ' , $ result );
72+ $ this ->assertSame (
73+ "Debugbar cleared. \n" ,
74+ preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()),
75+ );
76+ }
77+
78+ #[RequiresOperatingSystem('Darwin|Linux ' )]
79+ public function testClearDebugbarWithError (): void
80+ {
81+ $ path = WRITEPATH . 'debugbar ' . DIRECTORY_SEPARATOR . "debugbar_ {$ this ->time }.json " ;
82+
83+ // Attempt to make the file itself undeletable by setting the
84+ // immutable/uchg flag on supported platforms.
85+ $ immutableSet = false ;
86+ if (str_starts_with (PHP_OS , 'Darwin ' )) {
87+ @exec (sprintf ('chflags uchg %s ' , escapeshellarg ($ path )), $ output , $ rc );
88+ $ immutableSet = $ rc === 0 ;
89+ } else {
90+ // Try chattr on Linux with sudo (for containerized environments)
91+ @exec ('which chattr ' , $ whichOut , $ whichRc );
92+
93+ if ($ whichRc === 0 ) {
94+ @exec (sprintf ('sudo chattr +i %s ' , escapeshellarg ($ path )), $ output , $ rc );
95+ $ immutableSet = $ rc === 0 ;
96+ }
97+ }
98+
99+ if (! $ immutableSet ) {
100+ $ this ->markTestSkipped ('Cannot set file immutability in this environment ' );
101+ }
102+
103+ command ('debugbar:clear ' );
104+
105+ // Restore attributes so other tests are not affected.
106+ if (str_starts_with (PHP_OS , 'Darwin ' )) {
107+ @exec (sprintf ('chflags nouchg %s ' , escapeshellarg ($ path )));
108+ } else {
109+ @exec (sprintf ('sudo chattr -i %s ' , escapeshellarg ($ path )));
110+ }
111+
112+ $ this ->assertFileExists ($ path );
113+ $ this ->assertSame (
114+ "Error deleting the debugbar JSON files. \n" ,
115+ preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()),
116+ );
66117 }
67118}
0 commit comments