@@ -43,7 +43,7 @@ public function test_frontend_markup_is_injected_on_board_pages()
4343
4444 public function test_log_endpoint_rejects_invalid_json_payload ()
4545 {
46- $ payload = $ this ->extract_payload ( self :: request ( ' GET ' , ' index.php ' ) ? self :: get_content () : '' );
46+ $ payload = $ this ->fetch_frontend_payload ( );
4747
4848 self ::$ client ->request (
4949 'POST ' ,
@@ -63,22 +63,9 @@ public function test_log_endpoint_rejects_invalid_json_payload()
6363
6464 public function test_log_endpoint_accepts_valid_anonymous_submission_without_persisting_it ()
6565 {
66- $ payload = $ this ->extract_payload (self ::request ('GET ' , 'index.php ' ) ? self ::get_content () : '' );
66+ $ payload = $ this ->fetch_frontend_payload ();
67+ $ response = $ this ->post_log_request ($ payload , array ('analytics ' , 'analytics ' , 'unknown ' ));
6768
68- self ::$ client ->request (
69- 'POST ' ,
70- $ payload ['logEndpoint ' ],
71- array (),
72- array (),
73- array ('CONTENT_TYPE ' => 'application/json ' ),
74- json_encode (array (
75- 'hash ' => $ payload ['logHash ' ],
76- 'version ' => $ payload ['version ' ],
77- 'categories ' => array ('analytics ' , 'analytics ' , 'unknown ' ),
78- ))
79- );
80-
81- $ response = json_decode (self ::$ client ->getResponse ()->getContent (), true );
8269 $ this ->assertSame (200 , self ::$ client ->getResponse ()->getStatus ());
8370 $ this ->assertSame (array ('necessary ' , 'analytics ' ), $ response ['categories ' ]);
8471 $ this ->assertSame ($ payload ['version ' ], $ response ['version ' ]);
@@ -97,22 +84,9 @@ public function test_log_endpoint_persists_valid_authenticated_submission()
9784 $ this ->create_user ('consentuser ' );
9885 $ this ->login ('consentuser ' );
9986
100- $ payload = $ this ->extract_payload (self ::request ('GET ' , 'index.php ' ) ? self ::get_content () : '' );
101-
102- self ::$ client ->request (
103- 'POST ' ,
104- $ payload ['logEndpoint ' ],
105- array (),
106- array (),
107- array ('CONTENT_TYPE ' => 'application/json ' ),
108- json_encode (array (
109- 'hash ' => $ payload ['logHash ' ],
110- 'version ' => $ payload ['version ' ],
111- 'categories ' => array ('analytics ' , 'analytics ' , 'unknown ' ),
112- ))
113- );
87+ $ payload = $ this ->fetch_frontend_payload ();
88+ $ response = $ this ->post_log_request ($ payload , array ('analytics ' , 'analytics ' , 'unknown ' ));
11489
115- $ response = json_decode (self ::$ client ->getResponse ()->getContent (), true );
11690 $ this ->assertSame (200 , self ::$ client ->getResponse ()->getStatus ());
11791 $ this ->assertSame (array ('necessary ' , 'analytics ' ), $ response ['categories ' ]);
11892 $ this ->assertSame ($ payload ['version ' ], $ response ['version ' ]);
@@ -130,8 +104,20 @@ public function test_log_endpoint_persists_valid_authenticated_submission()
130104
131105 public function test_log_endpoint_rejects_stale_version ()
132106 {
133- $ payload = $ this ->extract_payload (self ::request ('GET ' , 'index.php ' ) ? self ::get_content () : '' );
107+ $ payload = $ this ->fetch_frontend_payload ();
108+ $ response = $ this ->post_log_request ($ payload , array ('analytics ' ), $ payload ['version ' ] + 1 );
109+
110+ $ this ->assertSame (409 , self ::$ client ->getResponse ()->getStatus ());
111+ $ this ->assertSame ('version_mismatch ' , $ response ['error ' ]);
112+ }
113+
114+ protected function fetch_frontend_payload ()
115+ {
116+ return $ this ->extract_payload (self ::request ('GET ' , 'index.php ' ) ? self ::get_content () : '' );
117+ }
134118
119+ protected function post_log_request (array $ payload , array $ categories , $ version = null )
120+ {
135121 self ::$ client ->request (
136122 'POST ' ,
137123 $ payload ['logEndpoint ' ],
@@ -140,14 +126,12 @@ public function test_log_endpoint_rejects_stale_version()
140126 array ('CONTENT_TYPE ' => 'application/json ' ),
141127 json_encode (array (
142128 'hash ' => $ payload ['logHash ' ],
143- 'version ' => $ payload ['version ' ] + 1 ,
144- 'categories ' => array ( ' analytics ' ) ,
129+ 'version ' => $ version ?? $ payload ['version ' ],
130+ 'categories ' => $ categories ,
145131 ))
146132 );
147133
148- $ response = json_decode (self ::$ client ->getResponse ()->getContent (), true );
149- $ this ->assertSame (409 , self ::$ client ->getResponse ()->getStatus ());
150- $ this ->assertSame ('version_mismatch ' , $ response ['error ' ]);
134+ return json_decode (self ::$ client ->getResponse ()->getContent (), true );
151135 }
152136
153137 protected function extract_payload ($ content )
0 commit comments