<?php
namespace Istlab\AdminCrud\Tests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Istlab\AdminCrud\AdminCrudServiceProvider;
use Orchestra\Testbench\TestCase;
class SmokeTest extends TestCase
{
protected function getPackageProviders($app)
{
return [AdminCrudServiceProvider::class];
}
protected function defineEnvironment($app)
{
$app['config']->set('app.key', 'base64:' . base64_encode(random_bytes(32)));
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
// テストでは認証・Gate を無効化
$app['config']->set('admin-crud.middleware', ['web']);
$app['config']->set('admin-crud.gate', null);
}
protected function setUp(): void
{
parent::setUp();
Schema::create('papers', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->tinyInteger('open')->default(0);
$table->timestamps();
});
DB::table('papers')->insert([
['title' => 'Hello', 'open' => 0, 'created_at' => now(), 'updated_at' => now()],
['title' => 'World', 'open' => 1, 'created_at' => now(), 'updated_at' => now()],
]);
}
public function test_route_names_and_uris_match_original(): void
{
$this->assertEquals(url('/admin_crud'), route('admin.crud'));
$this->assertEquals(url('/admin_crudcopy'), route('admin.crudcopy'));
$this->assertEquals(url('/admin_cruddelete'), route('admin.cruddelete'));
$this->assertEquals(url('/admin_crudchkdelete'), route('admin.crudchkdelete'));
$this->assertEquals(url('/admin_crudsetautoinc'), route('admin.crud_setautoinc'));
$this->assertEquals(url('/admin_crudnew'), route('admin.crudnew'));
$this->assertEquals(url('/admin_crudtruncate'), route('admin.crudtruncate'));
$this->assertEquals(url('/admin_crud'), route('admin.crudpost'));
}
public function test_table_list(): void
{
$this->get('/admin_crud')
->assertOk()
->assertSee('papers')
->assertSee('Crud Tables');
}
public function test_table_view(): void
{
$this->get('/admin_crud?table=papers')
->assertOk()
->assertSee('Hello')
->assertSee('World');
}
public function test_row_view(): void
{
$this->get('/admin_crud?table=papers&row=1')
->assertOk()
->assertSee('Hello');
}
public function test_whereby_filter_via_post(): void
{
$this->post('/admin_crud', ['table' => 'papers', 'whereBy__title' => 'Hell'])
->assertOk()
->assertSee('Hello')
->assertDontSee('World');
}
public function test_post_update_cell(): void
{
$this->post('/admin_crud', [
'table' => 'papers',
'field' => 'title',
'data_id' => 1,
'dtype' => 'varchar',
'val' => 'Updated',
'tdid' => 'title__1__varchar',
])->assertOk();
$this->assertEquals('Updated', DB::table('papers')->where('id', 1)->value('title'));
}
public function test_post_toggle_tinyint(): void
{
$this->post('/admin_crud', [
'table' => 'papers',
'field' => 'open',
'data_id' => 1,
'dtype' => 'tinyint',
'tdid' => 'open__1__tinyint',
])->assertOk();
$this->assertEquals(1, DB::table('papers')->where('id', 1)->value('open'));
}
public function test_post_rejects_unknown_table(): void
{
$this->post('/admin_crud', [
'table' => 'papers;DROP TABLE papers',
'field' => 'title',
'data_id' => 1,
'dtype' => 'varchar',
'val' => 'x',
'tdid' => 'x',
])->assertNotFound();
}
public function test_post_rejects_unknown_field(): void
{
$this->post('/admin_crud', [
'table' => 'papers',
'field' => 'nonexistent',
'data_id' => 1,
'dtype' => 'varchar',
'val' => 'x',
'tdid' => 'x',
])->assertStatus(400);
}
public function test_unknown_table_is_404(): void
{
$this->get('/admin_crud?table=no_such_table')->assertNotFound();
}
public function test_truncate(): void
{
$this->get('/admin_crudtruncate?table=papers')->assertRedirect();
$this->assertEquals(0, DB::table('papers')->count());
}
public function test_setautoinc(): void
{
$this->post('/admin_crudsetautoinc', ['table' => 'papers', 'autoinc' => 50])
->assertRedirect();
$seq = DB::table('sqlite_sequence')->where('name', 'papers')->value('seq');
$this->assertEquals(49, (int) $seq);
}
public function test_setautoinc_validation(): void
{
$this->post('/admin_crudsetautoinc', ['table' => 'papers', 'autoinc' => 0])
->assertRedirect()
->assertSessionHas('feedback.error');
}
public function test_new_without_model_just_redirects(): void
{
$this->get('/admin_crudnew?table=papers')->assertRedirect();
$this->assertEquals(2, DB::table('papers')->count());
}
}