Update Kelompok Tani
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Pertanian\KelompokTani;
|
||||
use App\Models\Pertanian\AsetKelompokTani;
|
||||
use App\Models\Pertanian\BantuanKelompokTani;
|
||||
use App\Models\User;
|
||||
use App\Services\Produktivitas\KelompokTaniService;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -17,6 +18,17 @@ use Illuminate\Support\ViewErrorBag;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
beforeEach(function () {
|
||||
Schema::create('activity_log', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('log_name')->nullable();
|
||||
$table->text('description');
|
||||
$table->nullableMorphs('subject');
|
||||
$table->nullableMorphs('causer');
|
||||
$table->string('event')->nullable();
|
||||
$table->json('properties')->nullable();
|
||||
$table->uuid('batch_uuid')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
foreach ([
|
||||
'master_provinsi' => null,
|
||||
'master_kabupaten_kota' => 'provinsi_id',
|
||||
@@ -126,6 +138,10 @@ beforeEach(function () {
|
||||
return $request;
|
||||
};
|
||||
$this->profileDom = function ($view) {
|
||||
$data = $view->getData();
|
||||
if (!array_key_exists('ringkasanProfil', $data)) {
|
||||
$view->with('ringkasanProfil', app(KelompokTaniService::class)->profileSummary($data['kelompokTani'] ?? null));
|
||||
}
|
||||
$dom = new DOMDocument();
|
||||
$previous = libxml_use_internal_errors(true);
|
||||
$dom->loadHTML($view->render());
|
||||
@@ -159,6 +175,85 @@ test('profile saves identity and membership together on creation and update', fu
|
||||
expect($response->getTargetUrl())->toBe(route('kelompok-tani.edit', $group->id) . '#section-pengurus');
|
||||
});
|
||||
|
||||
test('profile summary uses saved group details and preserves recorded zero totals', function () {
|
||||
DB::table('master_komoditas')->insert([
|
||||
['id' => 1, 'nama' => 'Kopi'], ['id' => 2, 'nama' => 'Kakao'], ['id' => 3, 'nama' => 'Vanili'],
|
||||
]);
|
||||
$group = ($this->kelompok)([
|
||||
'jumlah_pengurus' => 0, 'jumlah_anggota_terdaftar' => null, 'komoditas_id' => [1, '2', 3, 1],
|
||||
'status_legalitas' => 'Sudah ada SK dari desa/kecamatan',
|
||||
'jabatan_responden' => 'Ketua', 'kontak_responden' => '08123456789', 'tahun_berdiri' => 2020,
|
||||
]);
|
||||
$summary = app(KelompokTaniService::class)->profileSummary($group);
|
||||
expect($summary['judul'])->toBe('Kelompok A');
|
||||
expect($summary['lokasi'])->toBe('Desa A · Kecamatan A · Ngada');
|
||||
expect($summary['jenis'])->toBe('Kelompok Tani (Poktan)');
|
||||
expect($summary['pengurus'])->toBe('0');
|
||||
expect($summary['anggota'])->toBe('Belum terisi');
|
||||
expect($summary['komoditas'])->toBe(['Kakao', 'Kopi', 'Vanili']);
|
||||
expect($summary['legalitas']['label'])->toBe('Sudah ada SK');
|
||||
expect($summary['legalitas']['detail'])->toBe('Sudah ada SK dari desa/kecamatan');
|
||||
expect($summary['kontak'])->toBe('08123456789');
|
||||
});
|
||||
|
||||
test('all profile tabs receive the same saved summary with expandable commodities', function () {
|
||||
DB::table('master_komoditas')->insert([
|
||||
['id' => 1, 'nama' => 'Kopi'], ['id' => 2, 'nama' => 'Kakao'], ['id' => 3, 'nama' => 'Vanili'],
|
||||
]);
|
||||
$group = ($this->kelompok)(['jumlah_pengurus' => 4, 'jumlah_anggota_terdaftar' => 5, 'komoditas_id' => [1, 2, 3]]);
|
||||
$controller = app(SurveyKelompokTaniController::class);
|
||||
$expected = app(KelompokTaniService::class)->profileSummary($group);
|
||||
foreach (['identitas', 'kelembagaan', 'aktivitas-fungsi', 'sosial-konflik'] as $section) {
|
||||
$view = $controller->edit(($this->profileRequest)(['section' => $section], 'GET'), $group);
|
||||
expect($view->getData()['ringkasanProfil'])->toEqual($expected);
|
||||
$dom = ($this->profileDom)($view);
|
||||
expect($dom->query('//div[@id="kelompok-tani-summary"]/div[contains(@class,"card-footer")]')->length)->toBe(1);
|
||||
expect($dom->query('//div[contains(@class,"card-footer") and contains(@class,"border-top")]')->length)->toBe(1);
|
||||
expect($dom->query('//div[@id="kelompok-tani-summary"]/div[contains(@class,"card-body")]//*[@data-summary-field="tahun_berdiri"]')->length)->toBe(1);
|
||||
expect($dom->query('//div[contains(@class,"card-footer")]//*[@data-summary-field="tahun_berdiri"]')->length)->toBe(0);
|
||||
expect($dom->query('//div[contains(@class,"card-footer")]//*[@data-summary-field="diperbarui"]/time')->length)->toBe(1);
|
||||
expect($dom->query('//*[@data-summary-field="pengurus"]')->item(0)->textContent)->toBe('4');
|
||||
expect($dom->query('//*[@data-summary-field="anggota"]')->item(0)->textContent)->toBe('5');
|
||||
expect(trim($dom->query('//button[@data-bs-target="#kelompok-summary-extra-komoditas"]')->item(0)->textContent))->toBe('+1 lainnya');
|
||||
}
|
||||
});
|
||||
|
||||
test('new group summary is empty and rendering the header performs no database queries', function () {
|
||||
($this->profileRequest)([], 'GET');
|
||||
$summary = app(KelompokTaniService::class)->profileSummary();
|
||||
expect($summary['judul'])->toBe('Profil Kelompok Tani Baru');
|
||||
expect($summary['pengurus'])->toBe('Belum terisi');
|
||||
expect($summary['anggota'])->toBe('Belum terisi');
|
||||
expect($summary['komoditas'])->toBe([]);
|
||||
DB::enableQueryLog();
|
||||
DB::flushQueryLog();
|
||||
$html = view('produktivitas.survey-kelompok-tani.header', ['kelompokTani' => null, 'ringkasanProfil' => $summary])->render();
|
||||
expect(DB::getQueryLog())->toBe([]);
|
||||
expect($html)->toContain('Belum tersimpan');
|
||||
DB::disableQueryLog();
|
||||
});
|
||||
|
||||
test('summary update time includes changes to group sections and removal of a recorded aid', function () {
|
||||
$group = ($this->kelompok)();
|
||||
DB::table('survey_kelompok_tani')->where('id', $group->id)->update([
|
||||
'created_at' => '2026-01-01 08:00:00', 'updated_at' => '2026-01-01 08:00:00',
|
||||
]);
|
||||
$group->refresh();
|
||||
$service = app(KelompokTaniService::class);
|
||||
expect($service->profileSummary($group)['diperbarui']->format('Y-m-d H:i:s'))->toBe('2026-01-01 08:00:00');
|
||||
|
||||
foreach ([
|
||||
['survey_pengurus_kelompok_tani', ['nama_pengurus' => 'Ketua', 'jabatan' => 'Ketua', 'kontak' => '08123456789', 'status' => true, 'updated_at' => null], '2026-01-02 09:00:00'],
|
||||
['survey_aset_kelompok_tani', ['jenis_aset' => 'Timbangan', 'status_aset' => true, 'status_pengelolaan_aset' => 'Swadaya', 'kondisi_aset' => 'Berfungsi', 'updated_at' => '2026-01-03 10:00:00'], '2026-01-03 10:00:00'],
|
||||
['survey_bantuan_kelompok_tani', ['nama_program' => 'Program A', 'penyelenggara' => 'Dinas', 'bentuk_bantuan' => 'Bibit', 'updated_at' => '2026-01-04 11:00:00', 'deleted_at' => '2026-01-04 11:00:00'], '2026-01-04 11:00:00'],
|
||||
] as [$table, $attributes, $expected]) {
|
||||
DB::table($table)->insert(['uid' => 'TEST-UPDATED', 'survey_kelompok_tani_id' => $group->id, 'created_at' => $expected, ...$attributes]);
|
||||
expect($service->profileSummary($group)['diperbarui']->format('Y-m-d H:i:s'))->toBe($expected);
|
||||
}
|
||||
DB::table('survey_kelompok_tani')->where('id', $group->id)->update(['updated_at' => '2026-01-05 12:00:00']);
|
||||
expect($service->profileSummary($group->refresh())['diperbarui']->format('Y-m-d H:i:s'))->toBe('2026-01-05 12:00:00');
|
||||
});
|
||||
|
||||
test('invalid membership prevents partial profile saves', function () {
|
||||
$group = ($this->kelompok)(['jumlah_anggota_terdaftar' => 20]);
|
||||
$request = ($this->profileRequest)([
|
||||
|
||||
Reference in New Issue
Block a user