<?phpnamespace Tests;use App\Model\Id3Tag;use PHPUnit\Framework\TestCase;/** ID3v1 and ID3v2 tags of generated files: written and read back, every encoding, broken files. */final class Id3TagTest extends TestCase{ /** @var list<string> */ private array $files = []; protected function tearDown(): void { foreach ($this->files as $file) { @unlink($file); } } private function file(string $bytes): string { $file = sys_get_temp_dir() . '/wz-id3-' . bin2hex(random_bytes(4)) . '.mp3'; file_put_contents($file, $bytes); $this->files[] = $file; return $file; } /** Some MPEG audio: frames of 128 kbps, 44.1 kHz (417 bytes each). */ private static function audio(int $frames = 20): string { return str_repeat("\xFF\xFB\x90\x00" . str_repeat("\x55", 413), $frames); } private static function syncsafe(int $n): string { return chr(($n >> 21) & 0x7F) . chr(($n >> 14) & 0x7F) . chr(($n >> 7) & 0x7F) . chr($n & 0x7F); } /** An ID3v2 frame (2.3: plain size, 2.4: syncsafe). */ private static function frame(string $id, string $data, int $major = 3): string { return $id . ($major === 4 ? self::syncsafe(strlen($data)) : pack('N', strlen($data))) . "\0\0" . $data; } private static function v2(string $frames, int $major = 3, int $flags = 0): string { $frames .= str_repeat("\0", 64); return 'ID3' . chr($major) . "\0" . chr($flags) . self::syncsafe(strlen($frames)) . $frames; } public function testAnId3v1TagIsWrittenAtTheEndThenReplacedNotAddedAgain(): void { $audio = self::audio(); $file = $this->file($audio); $tag = new Id3Tag('Le titre', 'Les Artistes', 'L\'album', '1999', 'Un commentaire', Id3Tag::genreIndex('Hard Rock')); self::assertTrue($tag->write($file)); self::assertTrue($tag->present); self::assertSame(strlen($audio) + 128, filesize($file)); $read = Id3Tag::read($file); self::assertTrue($read->present); self::assertSame(['Le titre', 'Les Artistes', 'L\'album', '1999', 'Un commentaire', 79], [$read->title, $read->artist, $read->album, $read->year, $read->comment, $read->genre]); self::assertSame('Hard Rock', $read->genreName()); (new Id3Tag('Autre'))->write($file); self::assertSame(strlen($audio) + 128, filesize($file), 'the old tag replaced'); self::assertSame('Autre', Id3Tag::read($file)->title); self::assertSame($audio, substr((string)file_get_contents($file), 0, strlen($audio)), 'the sound untouched'); } public function testTheFieldsLongerThanThirtyAreCutAndTheAccentsKept(): void { $file = $this->file(self::audio()); (new Id3Tag(str_repeat('abcdefghij', 5), 'Hélène Ségara', 'Ça été', '20012', '€uro', 200))->write($file); $read = Id3Tag::read($file); self::assertSame(str_repeat('abcdefghij', 3), $read->title); self::assertSame('Hélène Ségara', $read->artist); self::assertSame('Ça été', $read->album); self::assertSame('2001', $read->year); self::assertSame('€uro', $read->comment, 'Windows 1252 has the euro'); self::assertSame(Id3Tag::OTHER, $read->genre, 'an unknown genre byte: Other'); } public function testALatin1FieldEndingWithAnAccentKeepsItsLastLetter(): void { $file = $this->file(self::audio()); (new Id3Tag('Café', 'Noël', 'Été', '2001', 'Déjà vu à Orléans'))->write($file); $read = Id3Tag::read($file); self::assertSame(['Café', 'Noël', 'Été', 'Déjà vu à Orléans'], [$read->title, $read->artist, $read->album, $read->comment]); // a full field of Latin-1 ending with an accent: not a UTF-8 letter cut (new Id3Tag(str_repeat('a', 29) . 'é'))->write($file); self::assertSame(str_repeat('a', 29) . 'é', Id3Tag::read($file)->title); } public function testAnId3v1WrittenInUtf8AndCutInTheMiddleOfALetterLosesOnlyThatLetter(): void { // what ffmpeg / yt-dlp write: UTF-8 cut at 30 bytes $title = substr(str_repeat('a', 28) . 'éé', 0, 30); // 28 + "é" (2 bytes) = 30: whole $cut = substr(str_repeat('a', 29) . 'é', 0, 30); // 29 + half of "é" $raw = 'TAG' . $title . str_pad($cut, 30, "\0") . str_repeat("\0", 30 + 4 + 30) . chr(17); $tag = Id3Tag::parse($raw); self::assertSame(str_repeat('a', 28) . 'é', $tag->title); self::assertSame(str_repeat('a', 29), $tag->artist); self::assertSame('Rock', $tag->genreName()); $euro = substr(str_repeat('b', 28) . '€', 0, 30); // 28 + 2 of the 3 bytes of "€" $accents = 'Été ' . str_repeat('c', 21) . 'é' . "\xC3"; // UTF-8 before the cut $raw = 'TAG' . $euro . $accents . str_repeat("\0", 30 + 4 + 30) . chr(0); $tag = Id3Tag::parse($raw); self::assertSame(str_repeat('b', 28), $tag->title); self::assertSame('Été ' . str_repeat('c', 21) . 'é', $tag->artist); } public function testAnId3v2TagInEachEncodingIsReadWhole(): void { $long = 'Un titre bien plus long que les trente lettres de ID3v1 — avec des accents éàü'; foreach ([3, 4] as $major) { $frames = self::frame('TIT2', "\x03" . $long, $major) // UTF-8 . self::frame('TPE1', "\x01\xFF\xFE" . mb_convert_encoding('Zoé & 東京', 'UTF-16LE', 'UTF-8') . "\0\0", $major) // UTF-16 + BOM . self::frame('TALB', "\x02" . mb_convert_encoding('Álbum', 'UTF-16BE', 'UTF-8'), $major) // UTF-16BE . self::frame('TYER', "\x00" . '2004', $major) . self::frame('COMM', "\x00" . 'fra' . "court\0" . "Le commentaire long en Latin-1 : " . "\xE9t\xE9", $major) . self::frame('TCON', "\x00(17)", $major); $file = $this->file(self::v2($frames, $major) . self::audio()); $tag = Id3Tag::read($file); self::assertTrue($tag->present, "v2.$major"); self::assertSame($long, $tag->title, "v2.$major"); self::assertSame('Zoé & 東京', $tag->artist, "v2.$major"); self::assertSame('Álbum', $tag->album, "v2.$major"); self::assertSame('2004', $tag->year, "v2.$major"); self::assertSame('Le commentaire long en Latin-1 : été', $tag->comment, "v2.$major"); self::assertSame('Rock', $tag->genreName(), "v2.$major: the genre (17) is Rock, not Other"); } } public function testTheGenresOfId3v2AsNumbersOrNames(): void { $genre = fn (string $text): string => Id3Tag::parseV2(3, 0, self::frame('TCON', "\x00" . $text))->genreName(); self::assertSame('Blues', $genre('(0)')); self::assertSame('Techno', $genre('18')); self::assertSame('Hard Rock', $genre('(79)Hard Rock')); self::assertSame('Other', $genre('(148)'), 'past the 80 of WinZik'); self::assertSame('Jazz', $genre('Jazz')); self::assertSame('Other', $genre('Musique de chambre')); } public function testAnId3v2Point2TagAndItsPicture(): void { $frame = fn (string $id, string $data): string => $id . substr(pack('N', strlen($data)), 1) . $data; $body = $frame('TT2', "\x00Vieux titre") . $frame('TP1', "\x00Vieil artiste") . $frame('PIC', "\x00JPG\x03desc\0JPEGDATA"); $tag = Id3Tag::parseV2(2, 0, $body . str_repeat("\0", 10)); self::assertSame('Vieux titre', $tag->title); self::assertSame('Vieil artiste', $tag->artist); self::assertSame('JPEGDATA', $tag->picture); $apic = Id3Tag::parseV2(3, 0, self::frame('APIC', "\x01image/png\0\x03" . "\xFF\xFE" . mb_convert_encoding('pochette', 'UTF-16LE', 'UTF-8') . "\0\0" . "\x89PNG")); self::assertSame("\x89PNG", $apic->picture); } public function testWinZiksMarksAreWrittenIntoTheId3v2TagAndTheOtherFramesKept(): void { $audio = self::audio(); $frames = self::frame('TIT2', "\x03Titre") . self::frame('TPE1', "\x03Artiste"); $file = $this->file(self::v2($frames) . $audio); self::assertTrue(Id3Tag::writeMarks($file, 1.5, 62.25)); $tag = Id3Tag::read($file); self::assertSame([1.5, 62.25], [$tag->markStart, $tag->markEnd]); self::assertSame(['Titre', 'Artiste'], [$tag->title, $tag->artist]); self::assertTrue(Id3Tag::writeMarks($file, null, 10.0), 'written again: replaced, not added'); $tag = Id3Tag::read($file); self::assertSame([null, 10.0], [$tag->markStart, $tag->markEnd]); self::assertSame(1, substr_count((string)file_get_contents($file), 'WINZIK_')); self::assertStringEndsWith($audio, (string)file_get_contents($file), 'the sound untouched'); // a file without any tag gets one; an unsynchronised one is left alone $plain = $this->file($audio); self::assertTrue(Id3Tag::writeMarks($plain, 3.0, null)); self::assertSame(3.0, Id3Tag::read($plain)->markStart); $unsync = $this->file(self::v2($frames, 3, 0x80) . $audio); self::assertFalse(Id3Tag::writeMarks($unsync, 3.0, null)); self::assertFalse(Id3Tag::writeMarks(sys_get_temp_dir() . '/wz-id3-missing.mp3', 1.0, 2.0)); } public function testBrokenAndTruncatedFilesGiveAnEmptyTagWithoutError(): void { $frames = self::frame('TIT2', "\x03Titre") . self::frame('APIC', "\x00image/jpeg\0\x03\0" . str_repeat('J', 300)); $whole = self::v2($frames) . self::audio(3) . (new Id3Tag('Fin'))->bytes(); $errors = []; set_error_handler(function (int $no, string $message) use (&$errors): bool { $errors[] = $message; return true; }); try { for ($len = 0; $len <= strlen($whole); $len += 7) { $tag = Id3Tag::read($this->file(substr($whole, 0, $len))); self::assertInstanceOf(Id3Tag::class, $tag); } // a header saying more than there is, frames with silly sizes, an empty frame, garbage Id3Tag::read($this->file('ID3' . "\x03\0\0" . "\x7F\x7F\x7F\x7F" . 'TIT2' . "\xFF\xFF\xFF\xFF\0\0x" . str_repeat("\0", 200))); Id3Tag::parseV2(4, 0, self::frame('TIT2', '', 4) . self::frame('COMM', "\x01eng", 4) . self::frame('APIC', "\x01", 4) . self::frame('TXXX', "\x01\xFF", 4)); Id3Tag::parseV2(3, 0x40, "\0\0\0\x06" . str_repeat("\0", 6) . self::frame('TIT2', "\x03Après l'en-tête")); Id3Tag::parseV2(3, 0x40, "\xFF\xFF"); Id3Tag::parseV2(3, 0, 'TIT2'); Id3Tag::parseV2(2, 0, 'TT2'); Id3Tag::parseV2(3, 0, random_bytes(500)); self::assertSame("Après l'en-tête", Id3Tag::parseV2(3, 0x40, "\0\0\0\x06" . str_repeat("\0", 6) . self::frame('TIT2', "\x03Après l'en-tête"))->title); self::assertFalse(Id3Tag::read(sys_get_temp_dir() . '/wz-id3-missing.mp3')->present); self::assertFalse(Id3Tag::read(sys_get_temp_dir())->present, 'a folder'); } finally { restore_error_handler(); } self::assertSame([], $errors, 'no warning'); }}