Unverified Commit bf45c9e5 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3301239 Improve the error message thrown in PoStreamReader::readLine()...

fix: #3301239 Improve the error message thrown in PoStreamReader::readLine() when translations cannot be saved

By: immaculatexavier
By: iperiba92
By: smustgrave
By: anybody
By: rcodina
By: matiasmiranda
By: pfrenssen
By: vladimiraus
By: hchonov
By: i-grou
By: guiu.rocafort.ferrer
By: teknocat
By: oily
By: longwave
By: grevil
By: alexpott
(cherry picked from commit 79cbaf53)
parent c4c50b2d
Loading
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -150,13 +150,14 @@ public function setURI($uri) {
   *   If the URI is not yet set.
   */
  public function open() {
    if (!empty($this->uri)) {
      $this->fd = fopen($this->uri, 'rb');
      $this->readHeader();
    }
    else {
    if (empty($this->uri)) {
      throw new \Exception('Cannot open stream without URI set.');
    }
    $this->fd = @fopen($this->uri, 'rb');
    if (!$this->fd) {
      throw new \RuntimeException('Cannot open stream for uri ' . $this->uri);
    }
    $this->readHeader();
  }

  /**
+30 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Component\Gettext;

use Drupal\Component\Gettext\PoStreamReader;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/**
 * Unit tests for the Gettext PO file header handling features.
 *
 * @see Drupal\Component\Gettext\PoHeader.
 */
#[Group('Gettext')]
class PoStreamReaderTest extends TestCase {

  /**
   * Validates that calling open with an invalid URI throws an exception.
   */
  public function testOpeningFileError(): void {
    $reader = new PoStreamReader();
    $reader->setURI('fake');
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('Cannot open stream for uri fake');
    $reader->open();
  }

}