/**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     foreach ($this->attributes as $item) {
         // Create attribute label
         $label = new AttributeLabel();
         $label->setValue($item['label']);
         // Create attribute
         $attribute = new Attribute();
         $attribute->setCode($item['code']);
         $attribute->setType($item['type']);
         $attribute->setSharingType(SharingType::GENERAL);
         $attribute->setLocalized($item['localized']);
         $attribute->setSystem($item['system']);
         $attribute->setRequired($item['required']);
         $attribute->setUnique($item['unique']);
         $attribute->addLabel($label);
         if (isset($item['options'])) {
             foreach ($item['options'] as $optionItem) {
                 $masterOption = new AttributeOption();
                 $masterOption->setValue($optionItem['value']);
                 $masterOption->setOrder($optionItem['order']);
                 $attribute->addOption($masterOption);
             }
         }
         $manager->persist($attribute);
     }
     if (!empty($this->attributes)) {
         $manager->flush();
         $manager->clear();
     }
 }
 /**
  * @dataProvider attributesDataProvider
  * @param string $type
  * @param string $code
  * @param boolean $localized
  * @param string $sharingGroup
  * @param array $validation
  * @param string $label
  * @param string $data
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function testAttributes($type, $code, $localized, $sharingGroup, $validation, $label, $data)
 {
     $crawler = $this->client->request('GET', $this->getUrl("{$this->formCreate}"));
     /** @var Form $form */
     $form = $crawler->selectButton('Continue')->form();
     $form["{$this->formCreate}[code]"] = $code;
     $form["{$this->formCreate}[type]"] = $type;
     $form["{$this->formCreate}[localized]"] = $localized;
     // Submit attribute create first step
     $this->client->followRedirects(true);
     $crawler = $this->client->submit($form);
     $result = $this->client->getResponse();
     $this->assertHtmlResponseStatusCodeEquals($result, 200);
     /** @var Form $form */
     $form = $crawler->selectButton('Save and Close')->form();
     // Check form values
     $formValues = $form->getValues();
     $this->assertContains($code, $formValues["{$this->formUpdate}[code]"]);
     $this->assertContains($type, $formValues["{$this->formUpdate}[type]"]);
     $this->assertEmpty($formValues["{$this->formUpdate}[label][default]"]);
     // Check labels
     $this->assertLocalize($formValues, 'label');
     if ($localized && !$this->isSelectType($type)) {
         // Check defaultValue for available locales
         $this->assertLocalize($formValues, 'defaultValue');
     } elseif (array_key_exists("{$this->formUpdate}[defaultValue]", $formValues)) {
         $this->assertEmpty($formValues["{$this->formUpdate}[defaultValue]"]);
     }
     $attributeType = $this->getAttributeTypeByName($formValues["{$this->formUpdate}[type]"]);
     $attributeProperties = $this->getAttributeTypePropertyFields($attributeType);
     foreach ($attributeProperties as $attributeProperty) {
         // Only this property is true by default
         if ($attributeProperty == 'onProductView') {
             $this->assertNotEmpty($formValues["{$this->formUpdate}[{$attributeProperty}][default]"]);
         } else {
             $this->assertArrayNotHasKey("{$this->formUpdate}[{$attributeProperty}][default]", $formValues);
         }
         if ($attributeProperty == 'containHtml') {
             $this->assertArrayNotHasKey($attributeProperty, $formValues);
         } else {
             foreach ($this->websiteRegistry as $website) {
                 $siteId = $website->getId();
                 $this->assertContains('system', $formValues["{$this->formUpdate}[{$attributeProperty}][websites][{$siteId}][fallback]"]);
             }
         }
     }
     // Set default label. This field is required.
     $form["{$this->formUpdate}[label][default]"] = $label;
     if ($this->isSelectType($type)) {
         // Set default for options. By default exists only one option
         foreach (array_slice($data['options'], 0, 1) as $key => $option) {
             $form["{$this->formUpdate}[defaultOptions][{$key}][default]"] = $option['default'];
             $form["{$this->formUpdate}[defaultOptions][{$key}][order]"] = $option['order'];
         }
     }
     // Submit attribute create second step
     $crawler = $this->client->submit($form);
     $result = $this->client->getResponse();
     $this->assertHtmlResponseStatusCodeEquals($result, 200);
     $this->assertContains("Attribute saved", $crawler->html());
     // Add second option for select and multiselect
     if ($this->isSelectType($type)) {
         $em = $this->getContainer()->get('doctrine')->getManagerForClass('OroB2BAttributeBundle:Attribute');
         $attribute = $this->getContainer()->get('doctrine')->getRepository('OroB2BAttributeBundle:Attribute')->findOneBy(['code' => 'color']);
         $masterOption = new AttributeOption();
         $masterOption->setValue('Black');
         $masterOption->setOrder(100);
         $attribute->addOption($masterOption);
         $locales = $this->getLocales();
         foreach ($locales as $locale) {
             $option = new AttributeOption();
             $option->setLocale($locale);
             $option->setFallback(FallbackType::SYSTEM);
             $option->setOrder($masterOption->getOrder());
             $attribute->addOption($option);
             $masterOption->addRelatedOption($option);
         }
         $em->flush($attribute);
     }
     $edit = $crawler->filter('.pull-right .edit-button')->link();
     $crawler = $this->client->click($edit);
     $this->assertEquals($code, $crawler->filter('h1.user-name')->html());
     /** @var Form $form */
     $form = $crawler->selectButton('Save and Close')->form();
     // Set sharing type
     $form["{$this->formUpdate}[sharingType]"] = $sharingGroup;
     // Set validation
     foreach ($validation as $key => $value) {
         $form["{$this->formUpdate}[{$key}]"] = $value;
     }
     foreach ($data['label'] as $localeName => $localeValue) {
         $locale = $this->localeRegistry[$localeName];
         $localeId = $locale->getId();
         foreach ($localeValue as $name => $value) {
             $form["{$this->formUpdate}[label][locales][{$localeId}][{$name}]"] = $value;
         }
     }
     if ($localized) {
         $this->setLocalizedData($type, $data, $form);
     } else {
         $this->setNotLocalizedData($type, $data, $form);
     }
     foreach ($data['additional'] as $attributePropertyName => $attributePropertyData) {
         foreach ($attributePropertyData as $siteName => $siteValue) {
             $website = $this->websiteRegistry[$siteName];
             $siteId = $website->getId();
             foreach ($siteValue as $name => $value) {
                 $form["{$this->formUpdate}[{$attributePropertyName}][websites][{$siteId}][{$name}]"] = $value;
             }
         }
     }
     // Submit attribute update and stay on page
     $form->setValues(['input_action' => 'save_and_stay']);
     $crawler = $this->client->submit($form);
     $this->assertContains("{$code} - Edit - Attributes - Product management", $crawler->html());
     $this->assertContains("Attribute saved", $crawler->html());
     $form = $crawler->selectButton('Save and Close')->form();
     $formValues = $form->getValues();
     // Check sharing type
     $this->assertEquals($sharingGroup, $formValues["{$this->formUpdate}[sharingType]"]);
     // Check validation
     foreach ($validation as $key => $value) {
         if ($value) {
             $this->assertEquals($value, $formValues["{$this->formUpdate}[{$key}]"]);
         }
     }
     foreach ($data['label'] as $localeName => $localeValue) {
         $locale = $this->localeRegistry[$localeName];
         $localeId = $locale->getId();
         foreach ($localeValue as $name => $value) {
             $this->assertEquals($value, $formValues["{$this->formUpdate}[label][locales][{$localeId}][{$name}]"]);
         }
     }
     if ($localized) {
         $this->assertLocalizedData($type, $data, $formValues);
     } else {
         $this->assertNotLocalizedData($type, $data, $formValues);
     }
     foreach ($data['additional'] as $attributePropertyName => $attributePropertyData) {
         foreach ($attributePropertyData as $siteName => $siteValue) {
             $website = $this->websiteRegistry[$siteName];
             $siteId = $website->getId();
             foreach ($siteValue as $name => $value) {
                 if ($value) {
                     $this->assertEquals($value, $formValues["{$this->formUpdate}[{$attributePropertyName}][websites][{$siteId}][{$name}]"]);
                 }
             }
         }
     }
     // Go to attribute grid
     $crawler = $this->client->request('GET', $this->getUrl('orob2b_attribute_index'));
     $result = $this->client->getResponse();
     $this->assertHtmlResponseStatusCodeEquals($result, 200);
     $this->assertContains($this->grid, $crawler->html());
     // Remove current attribute
     $response = $this->client->requestGrid($this->grid, [$this->grid . '[_filter][code][value]' => $code]);
     $result = $this->getJsonResponseContent($response, 200);
     $result = reset($result['data']);
     $this->assertEquals($code, $result['code']);
     $attributeId = (int) $result['id'];
     $this->client->request('DELETE', $this->getUrl('orob2b_api_delete_attribute', ['id' => $attributeId]));
     $result = $this->client->getResponse();
     $this->assertEmptyResponseStatusCodeEquals($result, 204);
     $this->client->request('GET', $this->getUrl('orob2b_attribute_view', ['id' => $attributeId]));
     $result = $this->client->getResponse();
     $this->assertHtmlResponseStatusCodeEquals($result, 404);
 }