114 lines
4.4 KiB
PHP
114 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Polls;
|
|
|
|
use App\Http\Livewire\Polls\PollForm;
|
|
use App\Mail\PollReceiptMail;
|
|
use App\Models\Admin;
|
|
use App\Models\Poll;
|
|
use App\Models\PollQuestion;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Livewire;
|
|
use Symfony\Component\Finder\Finder;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Every string the poll code translates exists in all language files.
|
|
*/
|
|
class PollTranslationsTest extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
private const LOCALES = ['en', 'nl', 'de', 'es', 'fr'];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// phpunit.xml configures Redis, which is the dev site's cache too
|
|
config(['cache.default' => 'array']);
|
|
}
|
|
|
|
/** @return array<int, string> */
|
|
private function pollStrings(): array
|
|
{
|
|
$finder = (new Finder())->files()->in([
|
|
app_path('Polls'),
|
|
app_path('Http/Livewire/Polls'),
|
|
resource_path('views/polls'),
|
|
resource_path('views/livewire/polls'),
|
|
resource_path('views/emails/polls'),
|
|
])->append([app_path('Mail/PollReceiptMail.php'), app_path('Http/Controllers/PollVerificationController.php'), resource_path('views/emails/newsletter/blocks/poll.blade.php')]);
|
|
|
|
$strings = [];
|
|
foreach ($finder as $file) {
|
|
preg_match_all('/(?:__|trans_choice)\(\s*([\'"])((?:\\\\.|(?!\1).)*)\1/s', file_get_contents($file->getPathname()), $matches, PREG_SET_ORDER);
|
|
foreach ($matches as $match) {
|
|
$string = stripslashes($match[2]);
|
|
if (!str_starts_with($string, 'validation.')) {
|
|
$strings[$string] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Used outside the poll folders: navigation menu and the posts table
|
|
$strings['Polls'] = true;
|
|
$strings['Edit poll'] = true;
|
|
$strings['Editorial'] = true;
|
|
$strings['Administration'] = true;
|
|
$strings['Log in to take part in this poll.'] = true;
|
|
$strings['Log in'] = true;
|
|
$strings['Poll'] = true;
|
|
$strings['Read-only'] = true;
|
|
$strings['Polls created (as draft)'] = true;
|
|
$strings['Not overwritten: poll has votes'] = true;
|
|
// Ballot list labels, picked from an array in polls/verify.blade.php
|
|
foreach (['Vote', 'Changed vote', 'Withdrawal', 'Seal'] as $label) {
|
|
$strings[$label] = true;
|
|
}
|
|
$strings['This post belongs to a poll that is no longer a draft, so its content cannot be changed. Settings of the poll can be changed on the Polls page.'] = true;
|
|
$strings['Read-only: this poll is no longer a draft'] = true;
|
|
|
|
return array_keys($strings);
|
|
}
|
|
|
|
public function test_all_poll_strings_are_translated(): void
|
|
{
|
|
$strings = $this->pollStrings();
|
|
$this->assertGreaterThan(50, count($strings), 'String extraction found too little');
|
|
|
|
foreach (self::LOCALES as $locale) {
|
|
$translations = json_decode(file_get_contents(lang_path("$locale.json")), true, 512, JSON_THROW_ON_ERROR);
|
|
$missing = array_values(array_diff($strings, array_keys($translations)));
|
|
$this->assertSame([], $missing, "Missing in $locale.json");
|
|
}
|
|
}
|
|
|
|
public function test_form_and_receipt_mail_render_in_dutch(): void
|
|
{
|
|
$postId = DB::table('posts')->insertGetId([
|
|
'postable_id' => 1, 'postable_type' => Admin::class, 'created_at' => now(), 'updated_at' => now(),
|
|
]);
|
|
$poll = Poll::create(['post_id' => $postId, 'status' => Poll::STATUS_OPEN]);
|
|
PollQuestion::create([
|
|
'poll_id' => $poll->id, 'type' => 'yes_no_conditional', 'required' => true,
|
|
'label' => ['en' => 'Monthly?', 'nl' => 'Maandelijks?'], 'settings' => [],
|
|
]);
|
|
$user = User::factory()->create(['lang_preference' => 'nl']);
|
|
$this->actingAs($user, 'web');
|
|
session(['activeProfileType' => User::class, 'activeProfileId' => $user->id]);
|
|
|
|
app()->setLocale('nl');
|
|
Livewire::test(PollForm::class, ['pollId' => $poll->id])
|
|
->assertSee('Maandelijks?')
|
|
->assertSee('Misschien, als...')
|
|
->assertDontSee('Ja, als...')
|
|
->assertSee('Stem uitbrengen');
|
|
|
|
$html = (new PollReceiptMail($poll, $user, 'ABCDE-FGHJK-MNPQR-STUVW'))->render();
|
|
$this->assertStringContainsString('Dit is je persoonlijke ontvangstcode', $html);
|
|
}
|
|
}
|