entity_test_create_bundle example

$field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    ]);

    $translatable_storage_setting = $this->randomString();
    $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
    $field_storage->save();

    $bundle = strtolower($this->randomMachineName());
    entity_test_create_bundle($bundle);
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => $bundle,
    ]);

    $translatable_field_setting = $this->randomString();
    $field->setSetting('translatable_field_setting', $translatable_field_setting);
    $field->save();

    $this->drupalLogin($this->translatorUser);

    

      $field_storage->save();
      $field_storages[] = $field_storage;
    }
    $bundles = [];
    for ($i = 0; $i < 2; $i++) {
      // For the sake of tablesort, make sure the second bundle is higher than       // the first one. Beware: MySQL is not case sensitive.       do {
        $bundle = $this->randomMachineName();
      } while ($bundles && strtolower($bundles[0]) >= strtolower($bundle));
      entity_test_create_bundle($bundle);
      foreach ($field_storages as $field_storage) {
        FieldConfig::create([
          'field_storage' => $field_storage,
          'bundle' => $bundle,
        ])->save();
      }
      $bundles[] = $bundle;
    }
    // Each unit is a list of field name, langcode and a column-value array.     $units[] = [$figures, 'en',
      [
        

  protected function setUp(): void {
    parent::setUp();
    $this->storage = $this->entityTypeManager->getStorage('entity_test');
  }

  /** * Tests making use of a custom bundle class. */
  public function testEntitySubclass() {
    entity_test_create_bundle('bundle_class');

    // Ensure we start life with empty counters.     $this->assertEquals(0, EntityTestBundleClass::$preCreateCount);
    $this->assertEquals(0, EntityTestBundleClass::$preDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postDeleteCount);
    $this->assertEquals(0, EntityTestBundleClass::$postLoadCount);
    $this->assertCount(0, EntityTestBundleClass::$postLoadEntitiesCount);

    // Verify statically created entity with bundle class returns correct class.     $entity = EntityTestBundleClass::create();
    $this->assertInstanceOf(EntityTestBundleClass::class$entity);

    
$this->drupalGet('entity_test/1');
    $this->assertSession()->elementExists('css', '.field--name-field-test-text');
  }

  /** * Copied from parent. * * This is Drupal\Tests\field_ui\FunctionalJavascript\EntityDisplayTest::testExtraFields() * with a line changed to reflect Claro's tabledrag selector. */
  public function testExtraFields() {
    entity_test_create_bundle('bundle_with_extra_fields');
    $this->drupalGet('entity_test/structure/bundle_with_extra_fields/display');
    $this->assertSession()->waitForElement('css', '.tabledrag-handle');
    $id = $this->getSession()->getPage()->find('css', '[name="form_build_id"]')->getValue();

    $extra_field_row = $this->getSession()->getPage()->find('css', '#display-extra-field');
    $disabled_region_row = $this->getSession()->getPage()->find('css', '.region-hidden-title');

    $extra_field_row->find('css', '.js-tabledrag-handle')->dragTo($disabled_region_row);
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()
      ->waitForElement('css', "[name='form_build_id']:not([value='$id'])");

    

  public function testOnBundleCreate() {
    $field_map = $this->container->get('entity_field.manager')->getFieldMap();
    $expected = [
      'entity_test' => 'entity_test',
    ];
    $this->assertEquals($expected$field_map['entity_test']['id']['bundles']);

    entity_test_create_bundle('custom');
    $field_map = $this->container->get('entity_field.manager')->getFieldMap();
    $expected = [
      'entity_test' => 'entity_test',
      'custom' => 'custom',
    ];
    $this->assertSame($expected$field_map['entity_test']['id']['bundles']);
  }

}
parent::setUp();
    $this->installSchema('user', ['users_data']);
    $this->installEntitySchema('entity_test_update');
    $this->moduleHandler = $this->container->get('module_handler');
    $this->database = $this->container->get('database');
  }

  /** * Tests making use of a custom bundle field. */
  public function testCustomBundleFieldUsage() {
    entity_test_create_bundle('custom', NULL, 'entity_test_update');

    // Check that an entity with bundle entity_test does not have the custom     // field.     $storage = $this->entityTypeManager->getStorage('entity_test_update');
    $entity = $storage->create([
      'type' => 'entity_test_update',
    ]);
    $this->assertFalse($entity->hasField('custom_bundle_field'));

    // Check that the custom bundle has the defined custom field and check     // saving and deleting of custom field data.
$this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('entity_test');

    $this->nodeType = NodeType::create([
      'type' => mb_strtolower($this->randomMachineName()),
      'name' => $this->randomString(),
    ]);
    $this->nodeType->save();

    // Create a custom bundle.     $this->customBundle = 'test_bundle_' . mb_strtolower($this->randomMachineName());
    entity_test_create_bundle($this->customBundle, NULL, 'entity_test');

    // Prepare the logger for collecting the expected critical error.     $this->container->get($this->testLogServiceName)->cleanLogs();
  }

  /** * Tests that config bundle deletions are mirrored in field config settings. */
  public function testConfigTargetBundleDeletion() {
    // Create two vocabularies.     /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
    
$this->assertEquals('2, 9, 9', $executable->getStyle()->getField(3, 'field_test_multiple'));
    $this->assertEquals('2', $executable->getStyle()->getField(3, 'field_test_multiple_1'));
    $this->assertEquals('9, 9', $executable->getStyle()->getField(3, 'field_test_multiple_2'));
  }

  /** * Tests that a field not available for every bundle is rendered as empty. */
  public function testMissingBundleFieldRender() {
    // Create a new bundle not having the test field attached.     $bundle = $this->randomMachineName();
    entity_test_create_bundle($bundle);

    $entity = EntityTest::create([
      'type' => $bundle,
      'name' => $this->randomString(),
      'user_id' => $this->testUsers[0]->id(),
    ]);
    $entity->save();

    $executable = Views::getView('test_field_field_test');
    $executable->execute();

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

    $this->setUpCurrentUser();
    $this->installEntitySchema('entity_test');

    // Create some testing bundles for 'entity_test' entity type.     entity_test_create_bundle('foo', 'Foo');
    entity_test_create_bundle('bar', 'Bar');
    entity_test_create_bundle('baz', 'Baz');
  }

  /** * Tests an entity route parameter having 'bundle' definition property. * * @covers ::convert */
  public function testRouteParamWithBundleDefinition(): void {
    $converter = $this->container->get('paramconverter.entity');

    
$entity->{$this->fieldName}->target_id = $referenced_entity->id();
    $violations = $entity->{$this->fieldName}->validate();
    $this->assertEquals(0, $violations->count(), 'Validation passes.');

    // Test an invalid reference.     $entity->{$this->fieldName}->target_id = 9999;
    $violations = $entity->{$this->fieldName}->validate();
    $this->assertEquals(1, $violations->count(), 'Validation throws a violation.');
    $this->assertEquals(t('The referenced entity (%type: %id) does not exist.', ['%type' => $this->referencedEntityType, '%id' => 9999])$violations[0]->getMessage());

    // Test a non-referenceable bundle.     entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType);
    $referenced_entity = $this->entityTypeManager
      ->getStorage($this->referencedEntityType)
      ->create(['type' => 'non_referenceable']);
    $referenced_entity->save();
    $entity->{$this->fieldName}->target_id = $referenced_entity->id();
    $violations = $entity->{$this->fieldName}->validate();
    $this->assertEquals(1, $violations->count(), 'Validation throws a violation.');
    $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', ['%type' => $this->referencedEntityType, '%id' => $referenced_entity->id()])$violations[0]->getMessage());
  }

  /** * Tests the multiple target entities loader. */
$this->grantPermissionsToTestedRole(['administer comments']);
        break;
    }
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create a "bar" bundle for the "entity_test" entity type and create.     $bundle = 'bar';
    entity_test_create_bundle($bundle, NULL, 'entity_test');

    // Create a comment field on this bundle.     $this->addDefaultCommentField('entity_test', 'bar', 'comment');

    // Create a "Camelids" test entity that the comment will be assigned to.     $commented_entity = EntityTest::create([
      'name' => 'Camelids',
      'type' => 'bar',
      'comment' => CommentItemInterface::OPEN,
    ]);
    $commented_entity->save();

    

  protected function createReferenceTestEntities($referenced_entity) {
    // All referencing entities should be of the type 'entity_test'.     $entity_type = 'entity_test';

    // Create a "foo" bundle for the given entity type.     $bundle = 'foo';
    entity_test_create_bundle($bundle, NULL, $entity_type);

    // Add a field of the given type to the given entity type's "foo" bundle.     $field_name = $referenced_entity->getEntityTypeId() . '_reference';
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'type' => 'entity_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => [
        'target_type' => $referenced_entity->getEntityTypeId(),
      ],
    ])
$this->installEntitySchema('taxonomy_term');

    // We want an entity reference field. It needs a vocabulary, terms, a field     // storage and a field. First, create the vocabulary.     $vocabulary = Vocabulary::create([
      'vid' => mb_strtolower($this->randomMachineName()),
    ]);
    $vocabulary->save();

    // Second, create the field.     entity_test_create_bundle('test_bundle');
    $this->fieldName = strtolower($this->randomMachineName());
    $handler_settings = [
      'target_bundles' => [
        $vocabulary->id() => $vocabulary->id(),
      ],
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);

    // Create two terms and also two accounts.     for ($i = 0; $i <= 1; $i++) {
      
    $this->container->get('config.manager')->uninstall('module', 'entity_test');
    $workflow = Workflow::load('editorial');
    $entity_types = $workflow->getTypePlugin()->getEntityTypes();
    $this->assertEquals([]$entity_types);
  }

  /** * Tests the content moderation workflow dependencies for non-config bundles. */
  public function testWorkflowNonConfigBundleDependencies() {
    // Create a bundle not based on any particular configuration.     entity_test_create_bundle('test_bundle');

    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test', 'test_bundle');
    $workflow->save();

    // Ensure the bundle is correctly added to the workflow.     $this->assertEquals([
      'module' => [
        'content_moderation',
        'entity_test',
      ],
    ],
$field_id_2a = "entity_test.entity_test.$field_name_2";
    $field_id_2b = "entity_test.test_bundle.$field_name_2";

    // Check that the field storages and fields do not exist yet.     $this->assertNull(FieldStorageConfig::load($field_storage_id));
    $this->assertNull(FieldConfig::load($field_id));
    $this->assertNull(FieldStorageConfig::load($field_storage_id_2));
    $this->assertNull(FieldConfig::load($field_id_2a));
    $this->assertNull(FieldConfig::load($field_id_2b));

    // Create a second bundle for the 'Entity test' entity type.     entity_test_create_bundle('test_bundle');

    // Enable field_test_config module and check that the field and storage     // shipped in the module's default config were created.     \Drupal::service('module_installer')->install(['field_test_config']);

    // A field storage with one single field.     $field_storage = FieldStorageConfig::load($field_storage_id);
    $this->assertNotEmpty($field_storage, 'The field was created.');
    $field = FieldConfig::load($field_id);
    $this->assertNotEmpty($field, 'The field was deleted.');

    
Home | Imprint | This part of the site doesn't use cookies.