Commit 35ef8ef5 authored by catch's avatar catch
Browse files

Issue #3317744 by idebr, smustgrave: Bundle restrictions from a route do not...

Issue #3317744 by idebr, smustgrave: Bundle restrictions from a route do not apply to revisionable entities

(cherry picked from commit 25b8da7b)
parent 33df911f
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -120,7 +120,16 @@ public function convert($value, $definition, $name, array $defaults) {
    // If the entity type is revisionable and the parameter has the
    // "load_latest_revision" flag, load the active variant.
    if (!empty($definition['load_latest_revision'])) {
      return $this->entityRepository->getActive($entity_type_id, $value);
      $entity = $this->entityRepository->getActive($entity_type_id, $value);

      if (
        !empty($definition['bundle']) &&
        $entity instanceof EntityInterface &&
        !in_array($entity->bundle(), $definition['bundle'], TRUE)
      ) {
        return NULL;
      }
      return $entity;
    }

    // Do not inject the context repository as it is not an actual dependency:
+62 −0
Original line number Diff line number Diff line
@@ -189,4 +189,66 @@ public function testConvertNonRevisionableEntityType() {
    $this->assertEquals($entity->id(), $converted->id());
  }

  /**
   * Tests an entity route parameter having 'bundle' definition property.
   *
   * @covers ::convert
   */
  public function testRouteParamWithBundleDefinition(): void {
    $entity1 = EntityTestMulRev::create([
      'name' => $this->randomString(),
      'type' => 'foo',
    ]);
    $entity1->save();
    $entity2 = EntityTestMulRev::create([
      'name' => $this->randomString(),
      'type' => 'bar',
    ]);
    $entity2->save();
    $entity3 = EntityTestMulRev::create([
      'name' => $this->randomString(),
      'type' => 'baz',
    ]);
    $entity3->save();

    $definition = [
      'type' => 'entity:entity_test_mulrev',
      'bundle' => [
        'foo',
        'bar',
      ],
      'load_latest_revision' => TRUE,
    ];

    // An entity whose bundle is in the definition list is converted.
    $converted = $this->converter->convert($entity1->id(), $definition, 'qux', []);
    $this->assertSame($entity1->id(), $converted->id());

    // An entity whose bundle is in the definition list is converted.
    $converted = $this->converter->convert($entity2->id(), $definition, 'qux', []);
    $this->assertSame($entity2->id(), $converted->id());

    // An entity whose bundle is missed from definition is not converted.
    $converted = $this->converter->convert($entity3->id(), $definition, 'qux', []);
    $this->assertNull($converted);

    // A non-existing entity returns NULL.
    $converted = $this->converter->convert('some-non-existing-entity-id', $definition, 'qux', []);
    $this->assertNull($converted);

    $definition = [
      'type' => 'entity:entity_test_mulrev',
    ];

    // Check that all entities are returned when 'bundle' is not defined.
    $converted = $this->converter->convert($entity1->id(), $definition, 'qux', []);
    $this->assertSame($entity1->id(), $converted->id());
    $converted = $this->converter->convert($entity2->id(), $definition, 'qux', []);
    $this->assertSame($entity2->id(), $converted->id());
    $converted = $this->converter->convert($entity3->id(), $definition, 'qux', []);
    $this->assertSame($entity3->id(), $converted->id());
    $converted = $this->converter->convert('some-non-existing-entity-id', $definition, 'qux', []);
    $this->assertNull($converted);
  }

}