/** * Tests simple IEF widget with different cardinality options. * * @throws \Exception */ protected function testSimpleCardinalityOptions() { $this->drupalLogin($this->user); $cardinality_options = [ 1 => 1, 2 => 2, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED => 3, ]; /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ $field_storage = $this->fieldStorageConfigStorage->load('node.single'); foreach ($cardinality_options as $cardinality => $limit) { $field_storage->setCardinality($cardinality); $field_storage->save(); $this->drupalGet('node/add/ief_simple_single'); $this->assertText('Single node', 'Inline entity field widget title found.'); $this->assertText('Reference a single node.', 'Inline entity field description found.'); $add_more_xpath = '//input[@data-drupal-selector="edit-single-add-more"]'; if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { $this->assertFieldByXPath($add_more_xpath, NULL, 'Add more button exists'); } else { $this->assertNoFieldByXPath($add_more_xpath, NULL, 'Add more button does NOT exist'); } $edit = ['title[0][value]' => 'Host node']; for ($item_number = 0; $item_number < $limit; $item_number++) { $edit["single[$item_number][inline_entity_form][title][0][value]"] = 'Child node nr.' . $item_number; if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { $next_item_number = $item_number + 1; $this->assertNoFieldByName("single[$next_item_number][inline_entity_form][title][0][value]", NULL, "Item $next_item_number does not appear before 'Add More' clicked"); if ($item_number < $limit - 1) { $this->drupalPostAjaxForm(NULL, $edit, 'single_add_more'); // This needed because the first time "add another item" is clicked it does not work // see https://www.drupal.org/node/2664626 if ($item_number == 0) { $this->drupalPostAjaxForm(NULL, $edit, 'single_add_more'); } $this->assertFieldByName("single[$next_item_number][inline_entity_form][title][0][value]", NULL, "Item $next_item_number does appear after 'Add More' clicked"); } } } $this->drupalPostForm(NULL, $edit, t('Save')); for ($item_number = 0; $item_number < $limit; $item_number++) { $this->assertText('Child node nr.' . $item_number, 'Label of referenced entity found.'); } } }
/** * @covers ::load() * @covers ::postLoad() * @covers ::mapFromStorageRecords() * @covers ::doLoadMultiple() */ public function testLoad() { $config_object = $this->getMockBuilder('Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->getMock(); $config_object->expects($this->exactly(2))->method('get')->will($this->returnValueMap(array(array('', array('id' => 'foo')), array('id', 'foo')))); $this->configFactory->expects($this->once())->method('loadMultiple')->with(array('the_config_prefix.foo'))->will($this->returnValue(array($config_object))); $this->moduleHandler->expects($this->exactly(2))->method('getImplementations')->will($this->returnValue(array())); $entity = $this->entityStorage->load('foo'); $this->assertInstanceOf('Drupal\\Core\\Entity\\EntityInterface', $entity); $this->assertSame('foo', $entity->id()); }