1717use CodeIgniter \Config \Services ;
1818use CodeIgniter \Superglobals ;
1919use CodeIgniter \Test \CIUnitTestCase ;
20- use CodeIgniter \Test \Filters \CITestStreamFilter ;
2120use CodeIgniter \Test \Mock \MockInputOutput ;
2221use CodeIgniter \Test \StreamFilterTrait ;
2322use PHPUnit \Framework \Attributes \Group ;
@@ -70,14 +69,6 @@ protected function tearDown(): void
7069 CLI ::reset ();
7170 }
7271
73- /**
74- * Gets buffer contents then releases it.
75- */
76- protected function getBuffer (): string
77- {
78- return $ this ->getStreamFilterBuffer ();
79- }
80-
8172 protected function resetEnvironment (): void
8273 {
8374 putenv ('encryption.key ' );
@@ -88,31 +79,35 @@ protected function resetEnvironment(): void
8879 public function testGenerateKeyShowsEncodedKey (): void
8980 {
9081 command ('key:generate --show ' );
91- $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
82+ $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getStreamFilterBuffer ());
9283
84+ $ this ->resetStreamFilterBuffer ();
9385 command ('key:generate --prefix base64 --show ' );
94- $ this ->assertStringContainsString ('base64: ' , $ this ->getBuffer ());
86+ $ this ->assertStringContainsString ('base64: ' , $ this ->getStreamFilterBuffer ());
9587
88+ $ this ->resetStreamFilterBuffer ();
9689 command ('key:generate --prefix hex2bin --show ' );
97- $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
90+ $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getStreamFilterBuffer ());
9891 }
9992
10093 #[PreserveGlobalState(false )]
10194 #[RunInSeparateProcess]
10295 public function testGenerateKeyCreatesNewKey (): void
10396 {
10497 command ('key:generate ' );
105- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
98+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
10699 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
107100 $ this ->assertStringContainsString ('hex2bin: ' , (string ) file_get_contents ($ this ->envPath ));
108101
102+ $ this ->resetStreamFilterBuffer ();
109103 command ('key:generate --prefix base64 --force ' );
110- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
104+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
111105 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
112106 $ this ->assertStringContainsString ('base64: ' , (string ) file_get_contents ($ this ->envPath ));
113107
108+ $ this ->resetStreamFilterBuffer ();
114109 command ('key:generate --prefix hex2bin --force ' );
115- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
110+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
116111 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
117112 $ this ->assertStringContainsString ('hex2bin: ' , (string ) file_get_contents ($ this ->envPath ));
118113 }
@@ -123,8 +118,9 @@ public function testDefaultShippedEnvIsMissing(): void
123118 command ('key:generate ' );
124119 rename (ROOTPATH . 'lostenv ' , ROOTPATH . 'env ' );
125120
126- $ this ->assertStringContainsString ('Both default shipped ' , $ this ->getBuffer ());
127- $ this ->assertStringContainsString ('Error in setting ' , $ this ->getBuffer ());
121+ $ this ->assertStringContainsString ('Both default shipped ' , $ this ->getStreamFilterBuffer ());
122+ $ this ->assertStringContainsString ('Here \'s your new key instead: ' , $ this ->getStreamFilterBuffer ());
123+ $ this ->assertStringNotContainsString ('Failed to write ' , $ this ->getStreamFilterBuffer ());
128124 }
129125
130126 /**
@@ -136,7 +132,7 @@ public function testKeyGenerateWhenKeyIsMissingInDotEnvFile(): void
136132
137133 command ('key:generate ' );
138134
139- $ this ->assertStringContainsString (' Application \' s new encryption key was successfully set. ' , $ this ->getBuffer ());
135+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
140136 $ this ->assertSame ("\nencryption.key = " . env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
141137 }
142138
@@ -152,9 +148,9 @@ public function testKeyGenerateWhenNewHexKeyIsSubsequentlyCommentedOut(): void
152148 ));
153149 $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
154150
155- CITestStreamFilter:: $ buffer = '' ;
151+ $ this -> resetStreamFilterBuffer () ;
156152 command ('key:generate --force ' );
157- $ this ->assertStringContainsString (' was successfully set. ' , $ this ->getBuffer ());
153+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
158154 $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
159155 }
160156
@@ -170,9 +166,9 @@ public function testKeyGenerateWhenNewBase64KeyIsSubsequentlyCommentedOut(): voi
170166 ));
171167 $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
172168
173- CITestStreamFilter:: $ buffer = '' ;
169+ $ this -> resetStreamFilterBuffer () ;
174170 command ('key:generate --force ' );
175- $ this ->assertStringContainsString (' was successfully set. ' , $ this ->getBuffer ());
171+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
176172 $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
177173 }
178174
@@ -190,15 +186,15 @@ public function testKeyGenerateReplacesUnloadedKeyInDotEnvFile(): void
190186 $ this ->assertSame ('' , env ('encryption.key ' , '' ));
191187
192188 command ('key:generate --force ' );
189+ $ this ->assertStringContainsString (sprintf ('New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
193190
194- $ this ->assertStringContainsString ('was successfully set. ' , $ this ->getBuffer ());
195-
196- $ contents = (string ) file_get_contents ($ this ->envPath );
191+ $ contents = @file_get_contents ($ this ->envPath );
192+ $ this ->assertIsString ($ contents , 'Failed to read .env file contents. ' );
197193 $ this ->assertStringNotContainsString ($ existingKey , $ contents );
198194 $ this ->assertStringContainsString ('encryption.key = ' . env ('encryption.key ' ), $ contents );
199195 }
200196
201- public function testKeyGenerateAbortsWhenOverwritePromptIsDeclined (): void
197+ public function testKeyGenerateCancelsWhenOverwritePromptIsDeclined (): void
202198 {
203199 command ('key:generate ' );
204200 $ key = env ('encryption.key ' , '' );
@@ -208,12 +204,13 @@ public function testKeyGenerateAbortsWhenOverwritePromptIsDeclined(): void
208204 $ io ->setInputs (['n ' ]);
209205 CLI ::setInputOutput ($ io );
210206
207+ $ this ->resetStreamFilterBuffer ();
211208 command ('key:generate ' );
212209
213210 $ this ->assertSame ($ key , env ('encryption.key ' , '' ), 'Existing key should not change. ' );
214211 $ this ->assertStringContainsString ($ key , (string ) file_get_contents ($ this ->envPath ));
215212 $ this ->assertStringContainsString ('Overwrite existing key? ' , $ io ->getOutput ());
216- $ this ->assertStringContainsString ('Setting new encryption key aborted . ' , $ io ->getOutput ());
213+ $ this ->assertStringContainsString ('Setting new encryption key cancelled . ' , $ io ->getOutput ());
217214 }
218215
219216 public function testKeyGenerateOverwritesWhenOverwritePromptIsConfirmed (): void
@@ -226,12 +223,13 @@ public function testKeyGenerateOverwritesWhenOverwritePromptIsConfirmed(): void
226223 $ io ->setInputs (['y ' ]);
227224 CLI ::setInputOutput ($ io );
228225
226+ $ this ->resetStreamFilterBuffer ();
229227 command ('key:generate --prefix base64 ' );
230228
231229 $ this ->assertNotSame ($ oldKey , env ('encryption.key ' , $ oldKey ));
232230 $ this ->assertStringContainsString ('base64: ' , (string ) file_get_contents ($ this ->envPath ));
233231 $ this ->assertStringContainsString ('Overwrite existing key? ' , $ io ->getOutput ());
234- $ this ->assertStringContainsString (' successfully set. ' , $ io ->getOutput ());
232+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ) , $ io ->getOutput ());
235233 }
236234
237235 #[PreserveGlobalState(false )]
@@ -243,19 +241,20 @@ public function testKeyGenerateAbortsNonInteractivelyWithExistingKey(): void
243241 $ this ->assertNotSame ('' , $ key );
244242
245243 $ this ->resetStreamFilterBuffer ();
246-
247244 command ('key:generate --no-interaction ' );
248245
249246 $ this ->assertSame ($ key , env ('encryption.key ' , '' ), 'Existing key should not change. ' );
250- $ this ->assertStringContainsString ('Setting new encryption key aborted. ' , $ this ->getBuffer ());
251- $ this ->assertStringContainsString ('--force ' , $ this ->getBuffer ());
247+ $ this ->assertStringContainsString (
248+ 'Setting new encryption key aborted: pass --force to overwrite the existing key in non-interactive mode. ' ,
249+ $ this ->getStreamFilterBuffer (),
250+ );
252251 }
253252
254253 public function testKeyGenerateErrorsOnInvalidPrefixNonInteractively (): void
255254 {
256255 command ('key:generate --prefix invalid --show --no-interaction ' );
257256
258- $ this ->assertStringContainsString ('Invalid prefix "invalid" ' , $ this ->getBuffer ());
257+ $ this ->assertStringContainsString ('Invalid prefix "invalid" ' , $ this ->getStreamFilterBuffer ());
259258 }
260259
261260 public function testKeyGeneratePromptsForInvalidPrefix (): void
@@ -269,4 +268,19 @@ public function testKeyGeneratePromptsForInvalidPrefix(): void
269268 $ this ->assertStringContainsString ('Please provide a valid prefix to use. ' , $ io ->getOutput ());
270269 $ this ->assertStringContainsString ('hex2bin: ' , $ io ->getOutput ());
271270 }
271+
272+ public function testKeyGenerateErrorsWhenEnvFileIsNotWritable (): void
273+ {
274+ command ('key:generate ' );
275+ chmod ($ this ->envPath , 0o444 );
276+
277+ try {
278+ $ this ->resetStreamFilterBuffer ();
279+ command ('key:generate --force ' );
280+
281+ $ this ->assertStringContainsString (sprintf ('Failed to write new encryption key to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
282+ } finally {
283+ chmod ($ this ->envPath , 0o644 );
284+ }
285+ }
272286}
0 commit comments