Commit efd43372 authored by Lee Rowlands's avatar Lee Rowlands Committed by catch
Browse files

Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm,...

Issue #2987548 by alexpott, johnv, euphoric_mv, HbtTundar, cilefen, xjm, samlerner, pratip.ghosh, jrochate, alezu: LogicException: The database connection is not serializable

(cherry picked from commit 743f5aa2)
(cherry picked from commit 72642535)
parent 7dbb440a
Loading
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@
 */
class LocaleTranslation implements TranslatorInterface, DestructableInterface {

  use DependencySerializationTrait;
  use DependencySerializationTrait {
    __sleep as traitSleep;
  }

  /**
   * Storage for strings.
@@ -161,4 +163,13 @@ public function destruct() {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep(): array {
    // ::$translations is an array of LocaleLookup objects, which have the
    // database service injected and therefore cannot be serialized safely.
    return array_diff($this->traitSleep(), ['translations']);
  }

}
+18 −0
Original line number Diff line number Diff line
@@ -20,12 +20,30 @@ class LocaleTranslationTest extends KernelTestBase {
    'locale',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installSchema('locale', [
      'locales_location',
      'locales_source',
      'locales_target',
    ]);
  }

  /**
   * Tests that \Drupal\locale\LocaleTranslation is serializable.
   */
  public function testSerializable(): void {
    /** @var \Drupal\locale\LocaleTranslation $translation */
    $translation = $this->container->get('string_translator.locale.lookup');
    $this->assertInstanceOf(LocaleTranslation::class, $translation);
    // Ensure that the \Drupal\locale\LocaleTranslation::$translations property
    // has some cached translations in it. Without this, serialization will not
    // actually be tested fully.
    $translation->getStringTranslation('es', 'test', '');

    // Prove that serialization and deserialization works without errors.
    $this->assertNotNull($translation);