setTables(array( 'xxx' => array(array('id' => 1, 'title' => 'Live title', 'date' => '2021-01-01 10:00:00'), array('id' => 2, 'title' => 'Other live', 'date' => '2021-01-02 10:00:00')), 'queue' => array(array('id' => 5, 'title' => 'Queued title', 'date' => '2021-02-01 10:00:00')), )); $this->token = $this->loginAs(); } private function story(array $fields): array { return $fields + array('title' => 'Changed', 'author' => 'Ann', 'story' => '
New text.
', 'csrf_token' => $this->token); } public function testVisitorsAreSentToTheLogin(): void { $this->cookies = array(); $this->assertRedirect('https://xxx.org.za/publish/login', $this->get('/publish/edit')); } public function testPlainHttpIsSentToHttps(): void { $this->assertRedirect('https://xxx.org.za/publish/edit?id=1', $this->get('/publish/edit?id=1', array('X-Test-Plain' => '1'))); } public function testTheQueueIsShownFirst(): void { $body = $this->get('/publish/edit')['body']; $this->assertStringContainsString('Queued title', $body); $this->assertStringNotContainsString('Live title', $body); $this->assertStringContainsString('click here', $body); } public function testTheLiveStoriesCanBeChosen(): void { $body = $this->get('/publish/edit?queue=0')['body']; $this->assertStringContainsString('Live title', $body); $this->assertStringContainsString('Other live', $body); $this->assertStringNotContainsString('Queued title', $body); } public function testAStoryOpensInTheForm(): void { $body = $this->get('/publish/edit?id=1&queue=0')['body']; $this->assertStringContainsString('', $body); $this->assertStringContainsString('', $body); $this->assertStringContainsString('value="delete"', $body); $this->assertSame($this->token, $this->csrfToken($body)); } public function testEditorsUpdateALiveStory(): void { $page = $this->post('/publish/edit', $this->story(array('action' => 'post', 'id' => '1', 'queue' => '0'))); $this->assertSame(200, $page['status']); $this->assertSame('Changed', $this->tables()['xxx'][0]['title']); $this->assertSame(array(), $this->mails()); } public function testPostingFromTheQueueMovesTheStory(): void { $this->post('/publish/edit', $this->story(array('action' => 'post', 'id' => '5', 'queue' => '1'))); $tables = $this->tables(); $this->assertSame(array(), $tables['queue']); $this->assertSame(array('Live title', 'Other live', 'Changed'), array_column($tables['xxx'], 'title')); } public function testSavingKeepsTheStoryInTheQueue(): void { $this->post('/publish/edit', $this->story(array('action' => 'save', 'id' => '5', 'queue' => '1', 'comments' => 'Needs work'))); $row = $this->tables()['queue'][0]; $this->assertSame(array('5', 'Changed', 'Needs work'), array($row['id'], $row['title'], $row['comments'])); } public function testDeletingNeedsTheToken(): void { $page = $this->post('/publish/edit', array('action' => 'delete', 'id' => '1', 'queue' => '0', 'csrf_token' => 'wrong')); $this->assertStringContainsString('Your session has expired.', $page['body']); $this->assertCount(2, $this->tables()['xxx']); $this->assertSame(array(), $this->queries('/^DELETE/')); } public function testDeletingAStory(): void { $page = $this->post('/publish/edit', array('action' => 'delete', 'id' => '1', 'queue' => '0', 'csrf_token' => $this->token)); $this->assertStringContainsString('Article has been deleted.', $page['body']); $this->assertSame(array('Other live'), array_column($this->tables()['xxx'], 'title')); } public function testDeletingCannotBeDoneByGet(): void { $this->get('/publish/edit?action=delete&id=1&queue=0&csrf_token=' . $this->token); $this->assertCount(2, $this->tables()['xxx']); } }