1010
1111namespace phpbb \boardannouncements \tests \controller ;
1212
13- class controller_test extends \phpbb_database_test_case
13+ use phpbb \boardannouncements \controller \controller ;
14+ use phpbb \boardannouncements \manager \manager ;
15+ use phpbb \boardannouncements \manager \nestedset ;
16+ use phpbb \config \config ;
17+ use phpbb \db \driver \driver_interface ;
18+ use phpbb \exception \http_exception ;
19+ use phpbb \language \language ;
20+ use phpbb \language \language_file_loader ;
21+ use phpbb \lock \db ;
22+ use phpbb_database_test_case ;
23+ use phpbb_mock_event_dispatcher ;
24+ use PHPUnit \DbUnit \DataSet \DefaultDataSet ;
25+ use PHPUnit \DbUnit \DataSet \IDataSet ;
26+ use PHPUnit \DbUnit \DataSet \XmlDataSet ;
27+ use phpbb \path_helper ;
28+ use phpbb \user ;
29+ use PHPUnit \Framework \Exception ;
30+ use PHPUnit \Framework \MockObject \MockObject ;
31+ use phpbb \datetime ;
32+ use phpbb \request \request ;
33+ use Symfony \Component \HttpFoundation \JsonResponse ;
34+
35+ class controller_test extends phpbb_database_test_case
1436{
1537 /**
1638 * Define the extensions to be tested
1739 *
1840 * @return array vendor/name of extension(s) to test
1941 */
20- protected static function setup_extensions ()
42+ protected static function setup_extensions (): array
2143 {
2244 return ['phpbb/boardannouncements ' ];
2345 }
2446
25- /** @var \phpbb\config\config */
26- protected $ config ;
27-
28- /** @var \phpbb\db\driver\driver_interface */
29- protected $ db ;
47+ protected config $ config ;
48+ protected driver_interface $ db ;
3049
3150 /**
3251 * Get data set fixtures
3352 */
34- public function getDataSet ()
53+ public function getDataSet (): IDataSet | XmlDataSet | DefaultDataSet
3554 {
3655 return $ this ->createXMLDataSet (__DIR__ . '/../fixtures/board_announcements.xml ' );
3756 }
@@ -44,7 +63,7 @@ protected function setUp(): void
4463 parent ::setUp ();
4564
4665 $ this ->db = $ this ->new_dbal ();
47- $ this ->config = new \ phpbb \ config \ config ([
66+ $ this ->config = new config ([
4867 'boardannouncements.table_lock.board_announcements_table ' => 0 ,
4968 'board_announcements_enable ' => 1 ,
5069 'enable_mod_rewrite ' => '0 ' ,
@@ -54,50 +73,60 @@ protected function setUp(): void
5473 /**
5574 * Create our controller
5675 */
57- protected function get_controller ($ user_id , $ is_registered , $ mode , $ ajax )
76+ protected function get_controller ($ user_id , $ is_registered , $ mode , $ ajax ): controller
5877 {
59- global $ user , $ phpbb_dispatcher , $ phpbb_path_helper , $ phpbb_root_path , $ phpEx ;
78+ global $ config , $ user , $ phpbb_dispatcher , $ phpbb_path_helper , $ phpbb_root_path , $ phpEx ;
79+
80+ $ config = new config ([]);
6081
61- $ phpbb_dispatcher = new \ phpbb_mock_event_dispatcher ();
62- $ phpbb_path_helper = $ this ->getMockBuilder (' \phpbb\ path_helper' )
82+ $ phpbb_dispatcher = new phpbb_mock_event_dispatcher ();
83+ $ phpbb_path_helper = $ this ->getMockBuilder (path_helper::class )
6384 ->disableOriginalConstructor ()
6485 ->getMock ();
6586
66- /** @var $user \PHPUnit\Framework\MockObject\MockObject|\phpbb\user */
67- $ user = $ this ->getMockBuilder ('\phpbb\user ' )
87+ $ phpbb_path_helper ->method ('update_web_root_path ' )
88+ ->willReturnArgument (0 );
89+
90+ /** @var $user MockObject|\phpbb\user */
91+ $ user = $ this ->getMockBuilder (user::class)
6892 ->setConstructorArgs ([
69- new \ phpbb \ language \ language (new \ phpbb \ language \ language_file_loader ($ phpbb_root_path , $ phpEx )),
70- ' \phpbb\ datetime' ,
93+ new language (new language_file_loader ($ phpbb_root_path , $ phpEx )),
94+ datetime::class ,
7195 ])
7296 ->getMock ();
7397 $ user ->data ['user_form_salt ' ] = '' ;
7498 $ user ->data ['user_id ' ] = $ user_id ;
7599 $ user ->data ['is_registered ' ] = $ is_registered ;
100+ $ user ->data ['session_page ' ] = "index. $ phpEx " ;
101+ $ user ->page ['page_dir ' ] = '' ;
76102
77- /** @var $request \PHPUnit\Framework\ MockObject\MockObject|\phpbb\request\ request */
78- $ request = $ this ->getMockBuilder (' \phpbb\ request\request ' )
103+ /** @var $request MockObject| request */
104+ $ request = $ this ->getMockBuilder (request::class )
79105 ->disableOriginalConstructor ()
80106 ->getMock ();
81107 $ request ->expects (self ::atMost (1 ))
82108 ->method ('is_ajax ' )
83109 ->willReturn ($ ajax
84110 );
85111 $ request ->method ('variable ' )
86- ->with (self ::anything ())
87- ->willReturnMap ([
88- ['hash ' , '' , false , \phpbb \request \request_interface::REQUEST , generate_link_hash ($ mode )]]
89- );
112+ ->willReturnCallback (function ($ name , $ default = '' ) use ($ user , $ mode ) {
113+ return match ($ name ) {
114+ 'redirect ' => $ user ->data ['session_page ' ],
115+ 'hash ' => generate_link_hash ($ mode ),
116+ default => $ default ,
117+ };
118+ });
90119
91- $ manager = new \ phpbb \ boardannouncements \ manager \ manager (
92- new \ phpbb \ boardannouncements \ manager \ nestedset (
120+ $ manager = new manager (
121+ new nestedset (
93122 $ this ->db ,
94- new \ phpbb \ lock \ db ('boardannouncements.table_lock.board_announcements_table ' , $ this ->config , $ this ->db ),
123+ new db ('boardannouncements.table_lock.board_announcements_table ' , $ this ->config , $ this ->db ),
95124 'phpbb_board_announcements ' ,
96125 'phpbb_board_announcements_track '
97126 )
98127 );
99128
100- return new \ phpbb \ boardannouncements \ controller \ controller (
129+ return new controller (
101130 $ manager ,
102131 $ request ,
103132 $ user
@@ -109,7 +138,7 @@ protected function get_controller($user_id, $is_registered, $mode, $ajax)
109138 *
110139 * @return array Test data
111140 */
112- public function controller_data ()
141+ public function controller_data (): array
113142 {
114143 return [
115144 [
@@ -140,7 +169,7 @@ public function controller_data()
140169 true ,
141170 200 ,
142171 '{"success":true,"id":1} ' , // True because a cookie was set
143- false , // Status should return false due to user not existing
172+ false , // Status should return false due to the user not existing
144173 ],
145174 [
146175 1 , // Announcement ID
@@ -162,20 +191,17 @@ public function controller_data()
162191 */
163192 public function test_controller ($ id , $ user_id , $ is_registered , $ mode , $ ajax , $ status_code , $ content , $ expected )
164193 {
165- // If non-ajax redirect is encountered, in testing it will trigger error
194+ // If a non-ajax redirect is encountered, in testing it will trigger_error/exception
166195 if (!$ ajax )
167196 {
168- // Throws E_WARNING in PHP 8.0+ and E_USER_WARNING in earlier versions
169- $ exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit \Framework \Error \Error::class : \PHPUnit \Framework \Error \Warning::class;
170- $ errno = PHP_VERSION_ID < 80000 ? E_USER_WARNING : E_WARNING ;
171- $ this ->expectException ($ exceptionName );
172- $ this ->expectExceptionCode ($ errno );
197+ $ this ->expectException (Exception::class);
198+ $ this ->expectExceptionMessage ('INSECURE_REDIRECT ' );
173199 }
174200
175201 $ controller = $ this ->get_controller ($ user_id , $ is_registered , $ mode , $ ajax );
176202
177203 $ response = $ controller ->close_announcement ($ id );
178- self ::assertInstanceOf (' \Symfony\Component\HttpFoundation\ JsonResponse' , $ response );
204+ self ::assertInstanceOf (JsonResponse::class , $ response );
179205 self ::assertEquals ($ status_code , $ response ->getStatusCode ());
180206 self ::assertEquals ($ content , $ response ->getContent ());
181207 self ::assertEquals ($ expected , $ this ->get_closed_announcements ($ id , $ user_id ));
@@ -186,7 +212,7 @@ public function test_controller($id, $user_id, $is_registered, $mode, $ajax, $st
186212 *
187213 * @return array Test data
188214 */
189- public function controller_fails_data ()
215+ public function controller_fails_data (): array
190216 {
191217 return [
192218 [ // test link hash fail
@@ -242,7 +268,7 @@ public function test_controller_fails($id, $user_id, $is_registered, $mode, $aja
242268 $ controller ->close_announcement ($ id );
243269 self ::fail ('The expected \phpbb\exception\http_exception was not thrown ' );
244270 }
245- catch (\ phpbb \ exception \ http_exception $ exception )
271+ catch (http_exception $ exception )
246272 {
247273 self ::assertEquals ($ status_code , $ exception ->getStatusCode ());
248274 self ::assertEquals ($ content , $ exception ->getMessage ());
@@ -256,7 +282,7 @@ public function test_controller_fails($id, $user_id, $is_registered, $mode, $aja
256282 * @param $user_id
257283 * @return bool
258284 */
259- protected function get_closed_announcements ($ id , $ user_id )
285+ protected function get_closed_announcements ($ id , $ user_id ): bool
260286 {
261287 $ ids = [];
262288 $ sql = 'SELECT announcement_id FROM phpbb_board_announcements_track WHERE user_id = ' . (int ) $ user_id ;
0 commit comments