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
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Xxx\Tests\Unit;
use Xxx\Tests\Support\FakeDb;
use Xxx\Tests\Support\SiteData;
use Xxx\Tests\Support\TestCase;
// delete_story() in publish/post.lib.php
class DeleteStoryTest extends TestCase {
protected function setUp(): void {
parent::setUp();
SiteData::install(array('xxx' => array(array('id' => 1), array('id' => 2)),
'queue' => array(array('id' => 1), array('id' => 3))));
open_db('open');
}
private function ids(string $table): array {
return array_map('intval', array_column(SiteData::$tables[$table], 'id'));
}
public function testVisitorsCannotDelete(): void {
$this->assertSame('', delete_story(1));
$this->assertSame(array(), FakeDb::matching('/^DELETE/'));
}
public function testEditorsDeleteFromTheLiveTable(): void {
$this->asEditor();
$this->assertSame('Article has been deleted.', delete_story(1));
$this->assertSame(array(2), $this->ids('xxx'));
$this->assertSame(array(1, 3), $this->ids('queue'));
}
public function testEditorsDeleteFromTheQueue(): void {
$this->asEditor();
$this->request(array('queue' => '1'));
$this->assertSame('Article has been deleted.', delete_story(3));
$this->assertSame(array(1), $this->ids('queue'));
$this->assertSame(array(1, 2), $this->ids('xxx'));
}
public function testAMoveDeletesTheQueueCopyQuietly(): void {
$this->asEditor();
$this->assertSame('', delete_story(1, TRUE));
$this->assertSame(array(3), $this->ids('queue'));
$this->assertSame(array(1, 2), $this->ids('xxx'));
}
public function testTheIdIsBoundAsAnInteger(): void {
$this->asEditor();
delete_story('2 OR 1=1');
$query = FakeDb::matching('/^DELETE/')[0];
$this->assertSame('DELETE FROM xxx WHERE `id` = ? LIMIT 1', $query['sql']);
$this->assertSame(array(2), $query['params']);
}
}