|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit; |
| 6 | + |
| 7 | +use App\Enums\NzbImportStatus; |
| 8 | +use App\Services\Nzb\NzbImportService; |
| 9 | +use Illuminate\Contracts\Console\Kernel; |
| 10 | +use Illuminate\Support\Facades\Cache; |
| 11 | +use Illuminate\Support\Facades\DB; |
| 12 | +use PDO; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +final class NzbImportServiceTest extends TestCase |
| 16 | +{ |
| 17 | + private string $databasePath; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var array<string, string|false> |
| 21 | + */ |
| 22 | + private array $originalEnvironment = []; |
| 23 | + |
| 24 | + public function createApplication() |
| 25 | + { |
| 26 | + $this->databasePath = sys_get_temp_dir().'/nntmux-nzb-import-test.sqlite'; |
| 27 | + |
| 28 | + $this->originalEnvironment = [ |
| 29 | + 'APP_ENV' => getenv('APP_ENV'), |
| 30 | + 'DB_CONNECTION' => getenv('DB_CONNECTION'), |
| 31 | + 'DB_DATABASE' => getenv('DB_DATABASE'), |
| 32 | + ]; |
| 33 | + |
| 34 | + if (file_exists($this->databasePath)) { |
| 35 | + unlink($this->databasePath); |
| 36 | + } |
| 37 | + |
| 38 | + $pdo = new PDO('sqlite:'.$this->databasePath); |
| 39 | + $pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)'); |
| 40 | + $pdo->exec("INSERT INTO settings (name, value) VALUES |
| 41 | + ('categorizeforeign', '0'), |
| 42 | + ('catwebdl', '0'), |
| 43 | + ('title', 'NNTmux Test'), |
| 44 | + ('home_link', '/')"); |
| 45 | + |
| 46 | + $this->setEnvironmentValue('APP_ENV', 'testing'); |
| 47 | + $this->setEnvironmentValue('DB_CONNECTION', 'sqlite'); |
| 48 | + $this->setEnvironmentValue('DB_DATABASE', $this->databasePath); |
| 49 | + |
| 50 | + $app = require __DIR__.'/../../bootstrap/app.php'; |
| 51 | + |
| 52 | + $app->make(Kernel::class)->bootstrap(); |
| 53 | + |
| 54 | + return $app; |
| 55 | + } |
| 56 | + |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + parent::setUp(); |
| 60 | + |
| 61 | + config([ |
| 62 | + 'database.default' => 'sqlite', |
| 63 | + 'database.connections.sqlite.database' => $this->databasePath, |
| 64 | + 'app.key' => 'base64:'.base64_encode(random_bytes(32)), |
| 65 | + ]); |
| 66 | + |
| 67 | + DB::purge(); |
| 68 | + DB::reconnect(); |
| 69 | + Cache::flush(); |
| 70 | + } |
| 71 | + |
| 72 | + protected function tearDown(): void |
| 73 | + { |
| 74 | + if ($this->databasePath !== '' && file_exists($this->databasePath)) { |
| 75 | + unlink($this->databasePath); |
| 76 | + } |
| 77 | + |
| 78 | + parent::tearDown(); |
| 79 | + |
| 80 | + foreach ($this->originalEnvironment as $key => $value) { |
| 81 | + $this->setEnvironmentValue($key, $value === false ? null : $value); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function test_begin_import_uses_specific_messages_and_counts_duplicates_separately(): void |
| 86 | + { |
| 87 | + $duplicateFile = $this->makeNzbFile('duplicate'); |
| 88 | + $blacklistedFile = $this->makeNzbFile('blacklisted'); |
| 89 | + $noGroupFile = $this->makeNzbFile('nogroup'); |
| 90 | + $failedFile = $this->makeNzbFile('failed'); |
| 91 | + |
| 92 | + $service = new class(['Browser' => true], [NzbImportStatus::Duplicate, NzbImportStatus::Blacklisted, NzbImportStatus::NoGroup, NzbImportStatus::Failed]) extends NzbImportService |
| 93 | + { |
| 94 | + /** |
| 95 | + * @param array<NzbImportStatus> $statuses |
| 96 | + */ |
| 97 | + public function __construct(array $options, private array $statuses) |
| 98 | + { |
| 99 | + parent::__construct($options); |
| 100 | + } |
| 101 | + |
| 102 | + protected function getAllGroups(): bool |
| 103 | + { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): NzbImportStatus |
| 108 | + { |
| 109 | + $status = array_shift($this->statuses) ?? NzbImportStatus::Failed; |
| 110 | + |
| 111 | + match ($status) { |
| 112 | + NzbImportStatus::Duplicate => $this->echoOut('This release is already in our DB so skipping: duplicate subject'), |
| 113 | + NzbImportStatus::Blacklisted => $this->echoOut('Subject is blacklisted: blacklisted subject'), |
| 114 | + NzbImportStatus::NoGroup => $this->echoOut('No group found for missing-group subject (one of alt.test are missing'), |
| 115 | + default => null, |
| 116 | + }; |
| 117 | + |
| 118 | + return $status; |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + $result = $service->beginImport( |
| 123 | + [$duplicateFile, $blacklistedFile, $noGroupFile, $failedFile], |
| 124 | + delete: false, |
| 125 | + deleteFailed: true, |
| 126 | + ); |
| 127 | + |
| 128 | + $this->assertIsString($result); |
| 129 | + $this->assertStringContainsString('This release is already in our DB so skipping: duplicate subject', $result); |
| 130 | + $this->assertStringContainsString('Subject is blacklisted: blacklisted subject', $result); |
| 131 | + $this->assertStringContainsString('No group found for missing-group subject (one of alt.test are missing', $result); |
| 132 | + $this->assertSame(1, substr_count($result, 'ERROR: Failed to insert NZB!')); |
| 133 | + $this->assertStringContainsString('Processed 0 NZBs in ', $result); |
| 134 | + $this->assertStringContainsString('3 NZBs were skipped, 1 were duplicates.', $result); |
| 135 | + |
| 136 | + $this->assertFileDoesNotExist($duplicateFile); |
| 137 | + $this->assertFileDoesNotExist($blacklistedFile); |
| 138 | + $this->assertFileDoesNotExist($noGroupFile); |
| 139 | + $this->assertFileDoesNotExist($failedFile); |
| 140 | + } |
| 141 | + |
| 142 | + public function test_begin_import_deletes_duplicate_blacklisted_and_no_group_files_when_delete_is_enabled(): void |
| 143 | + { |
| 144 | + $duplicateFile = $this->makeNzbFile('duplicate-delete'); |
| 145 | + $blacklistedFile = $this->makeNzbFile('blacklisted-delete'); |
| 146 | + $noGroupFile = $this->makeNzbFile('nogroup-delete'); |
| 147 | + |
| 148 | + $service = new class(['Browser' => true], [NzbImportStatus::Duplicate, NzbImportStatus::Blacklisted, NzbImportStatus::NoGroup]) extends NzbImportService |
| 149 | + { |
| 150 | + /** |
| 151 | + * @param array<NzbImportStatus> $statuses |
| 152 | + */ |
| 153 | + public function __construct(array $options, private array $statuses) |
| 154 | + { |
| 155 | + parent::__construct($options); |
| 156 | + } |
| 157 | + |
| 158 | + protected function getAllGroups(): bool |
| 159 | + { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + protected function scanNZBFile(mixed &$nzbXML, mixed $nzbFileName = '', mixed $source = ''): NzbImportStatus |
| 164 | + { |
| 165 | + $status = array_shift($this->statuses) ?? NzbImportStatus::Failed; |
| 166 | + |
| 167 | + match ($status) { |
| 168 | + NzbImportStatus::Duplicate => $this->echoOut('This release is already in our DB so skipping: duplicate subject'), |
| 169 | + NzbImportStatus::Blacklisted => $this->echoOut('Subject is blacklisted: blacklisted subject'), |
| 170 | + NzbImportStatus::NoGroup => $this->echoOut('No group found for missing-group subject (one of alt.test are missing'), |
| 171 | + default => null, |
| 172 | + }; |
| 173 | + |
| 174 | + return $status; |
| 175 | + } |
| 176 | + }; |
| 177 | + |
| 178 | + $result = $service->beginImport( |
| 179 | + [$duplicateFile, $blacklistedFile, $noGroupFile], |
| 180 | + delete: true, |
| 181 | + deleteFailed: false, |
| 182 | + ); |
| 183 | + |
| 184 | + $this->assertIsString($result); |
| 185 | + $this->assertStringNotContainsString('ERROR: Failed to insert NZB!', $result); |
| 186 | + $this->assertStringContainsString('2 NZBs were skipped, 1 were duplicates.', $result); |
| 187 | + |
| 188 | + $this->assertFileDoesNotExist($duplicateFile); |
| 189 | + $this->assertFileDoesNotExist($blacklistedFile); |
| 190 | + $this->assertFileDoesNotExist($noGroupFile); |
| 191 | + } |
| 192 | + |
| 193 | + private function makeNzbFile(string $suffix): string |
| 194 | + { |
| 195 | + $path = sys_get_temp_dir().'/'.$suffix.'-'.bin2hex(random_bytes(5)).'.nzb'; |
| 196 | + file_put_contents($path, '<nzb></nzb>'); |
| 197 | + |
| 198 | + return $path; |
| 199 | + } |
| 200 | + |
| 201 | + private function setEnvironmentValue(string $key, ?string $value): void |
| 202 | + { |
| 203 | + if ($value === null) { |
| 204 | + putenv($key); |
| 205 | + unset($_ENV[$key], $_SERVER[$key]); |
| 206 | + |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + putenv("{$key}={$value}"); |
| 211 | + $_ENV[$key] = $value; |
| 212 | + $_SERVER[$key] = $value; |
| 213 | + } |
| 214 | +} |
0 commit comments