Add PHP unit tests and page tests.

Run "composer install", then "composer test".
This commit is contained in:
tim
2026-09-26 03:48:09 +02:00
parent 848c447c5b
commit ed959888b4
42 changed files with 5522 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
<?php
namespace Xxx\Tests\Pages;
use Xxx\Tests\Support\PageTestCase;
// publish/edit.php, for editors
class EditTest extends PageTestCase {
private string $token;
protected function setUp(): void {
parent::setUp();
$this->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' => '<p>New text.</p>', '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('<a href="edit?queue=0">click here</a>', $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('<input type="text" id="title" name="title" maxlength="150" value="Live title" />', $body);
$this->assertStringContainsString('<input type="hidden" id="editor" name="editor" value="ed" />', $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']);
}
}