/**
  * {@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();
     }
 }
 /**
  * @expectedException \LogicException
  * @expectedExceptionMessage Several related attribute options found by the same locale ID.
  */
 public function testGetRelatedOptionByLocaleIdException()
 {
     $locale = $this->createLocale(1);
     $firstOption = new AttributeOption();
     $firstOption->setLocale($locale);
     $secondOption = new AttributeOption();
     $secondOption->setLocale($locale);
     $masterOption = new AttributeOption();
     $masterOption->addRelatedOption($firstOption)->addRelatedOption($secondOption);
     $masterOption->getRelatedOptionByLocaleId(1);
 }
 public function testProperties()
 {
     $locale = new Locale();
     $locale->setCode('es_MX');
     $attribute = new Attribute();
     $attribute->setType('float');
     $option = new AttributeOption();
     $option->setAttribute($attribute);
     $option->setLocale($locale);
     $properties = [['id', 1], ['integer', 3], ['string', 'string'], ['float', 3.9999], ['datetime', new \DateTime('now')], ['text', 'text'], ['fallback', 'website'], ['locale', $locale, false], ['locale', null], ['attribute', $attribute, false], ['option', $option, false]];
     $this->assertPropertyAccessors(new AttributeDefaultValue(), $properties);
 }
 /**
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $attribute = new Attribute();
     $attribute->setCode(self::ATTRIBUTE_CODE)->setType(Select::NAME)->setSharingType(SharingType::GENERAL);
     foreach (self::$options as $localeCode => $options) {
         $locale = $localeCode ? $this->getLocaleByCode($manager, $localeCode) : null;
         foreach ($options as $order => $value) {
             $option = new AttributeOption();
             $option->setLocale($locale)->setValue($value)->setOrder($order);
             $attribute->addOption($option);
         }
     }
     $manager->persist($attribute);
     $manager->flush($attribute);
     $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);
 }
 /**
  * @param AttributeOption $option
  * @return int|null
  */
 protected function getOptionLocaleId(AttributeOption $option)
 {
     return $option->getLocale() ? $option->getLocale()->getId() : null;
 }
 /**
  * @param AttributeOption|null $option
  * @return $this
  */
 public function setMasterOption(AttributeOption $option = null)
 {
     $this->masterOption = $option;
     if ($option) {
         $this->attribute = $option->getAttribute();
     }
     return $this;
 }
 /**
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testAttributeRelations()
 {
     // Create websites
     $firstWebsite = new Website();
     $firstWebsite->setName('First Website');
     $firstWebsite->setUrl('www.first-website.com');
     $secondWebsite = new Website();
     $secondWebsite->setName('Second Website');
     $secondWebsite->setUrl('www.second-website.com');
     $thirdWebsite = new Website();
     $thirdWebsite->setName('Third Website');
     $thirdWebsite->setUrl('www.third-website.com');
     // Create locales
     $localeOne = new Locale();
     $localeOne->setCode('es_MX');
     $localeTwo = new Locale();
     $localeTwo->setCode('en_GB');
     $localeThree = new Locale();
     $localeThree->setCode('en_AU');
     // Create attribute labels
     $labelOne = new AttributeLabel();
     $labelOne->setValue('Attribute 01');
     $labelOne->setLocale($localeOne);
     $labelTwo = new AttributeLabel();
     $labelTwo->setValue('Attribute 02');
     $labelTwo->setLocale($localeTwo);
     $labelThree = new AttributeLabel();
     $labelThree->setValue('Attribute 03');
     $labelThree->setLocale($localeThree);
     // Create attribute
     $attribute = new Attribute();
     $attribute->setType('select');
     // Create attribute properties
     $propertyOne = new AttributeProperty();
     $propertyOne->setAttribute($attribute);
     $propertyOne->setWebsite($firstWebsite);
     $propertyTwo = new AttributeProperty();
     $propertyTwo->setAttribute($attribute);
     $propertyTwo->setWebsite($secondWebsite);
     $propertyThree = new AttributeProperty();
     $propertyThree->setAttribute($attribute);
     $propertyThree->setWebsite($thirdWebsite);
     // Create options
     $optionOne = new AttributeOption();
     $optionOne->setAttribute($attribute);
     $optionOne->setLocale($localeOne);
     $optionTwo = new AttributeOption();
     $optionTwo->setAttribute($attribute);
     $optionTwo->setLocale($localeTwo);
     $optionThree = new AttributeOption();
     $optionThree->setAttribute($attribute);
     $optionThree->setLocale($localeThree);
     // Create default values
     $defaultValueOne = new AttributeDefaultValue();
     $defaultValueOne->setAttribute($attribute);
     $defaultValueOne->setLocale($localeOne);
     $defaultValueOne->setOption($optionOne);
     $defaultValueTwo = new AttributeDefaultValue();
     $defaultValueTwo->setAttribute($attribute);
     $defaultValueTwo->setLocale($localeTwo);
     $defaultValueTwo->setOption($optionTwo);
     $defaultValueThree = new AttributeDefaultValue();
     $defaultValueThree->setAttribute($attribute);
     $defaultValueThree->setLocale($localeThree);
     $defaultValueThree->setOption($optionThree);
     // Reset labels
     $this->assertSame($attribute, $attribute->resetLabels([$labelOne, $labelTwo]));
     $actual = $attribute->getLabels();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$labelOne, $labelTwo], $actual->toArray());
     foreach ($actual as $label) {
         $this->assertContains($label, $attribute->getLabels());
     }
     // Reset properties
     $this->assertSame($attribute, $attribute->resetProperties([$propertyOne, $propertyTwo]));
     $actual = $attribute->getProperties();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$propertyOne, $propertyTwo], $actual->toArray());
     foreach ($actual as $property) {
         $this->assertContains($property, $attribute->getProperties());
     }
     // Reset options
     $this->assertSame($attribute, $attribute->resetOptions([$optionOne, $optionTwo]));
     $actual = $attribute->getOptions();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$optionOne, $optionTwo], $actual->toArray());
     foreach ($actual as $option) {
         $this->assertContains($option, $attribute->getOptions());
     }
     // Reset default values
     $this->assertSame($attribute, $attribute->resetDefaultValues([$defaultValueOne, $defaultValueTwo]));
     $actual = $attribute->getDefaultValues();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$defaultValueOne, $defaultValueTwo], $actual->toArray());
     foreach ($actual as $defaultValue) {
         $this->assertContains($defaultValue, $attribute->getDefaultValues());
     }
     // Add already added label
     $this->assertSame($attribute, $attribute->addLabel($labelTwo));
     $actual = $attribute->getLabels();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$labelOne, $labelTwo], $actual->toArray());
     // Add already added  property
     $this->assertSame($attribute, $attribute->addProperty($propertyTwo));
     $actual = $attribute->getProperties();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$propertyOne, $propertyTwo], $actual->toArray());
     // Add already added  option
     $this->assertSame($attribute, $attribute->addOption($optionTwo));
     $actual = $attribute->getOptions();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$optionOne, $optionTwo], $actual->toArray());
     // Add already added  default value
     $this->assertSame($attribute, $attribute->addDefaultValue($defaultValueTwo));
     $actual = $attribute->getDefaultValues();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$defaultValueOne, $defaultValueTwo], $actual->toArray());
     // Add new label
     $this->assertSame($attribute, $attribute->addLabel($labelThree));
     $actual = $attribute->getLabels();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$labelOne, $labelTwo, $labelThree], $actual->toArray());
     foreach ($actual as $label) {
         $this->assertContains($label, $attribute->getLabels());
     }
     // Add new property
     $this->assertSame($attribute, $attribute->addProperty($propertyThree));
     $actual = $attribute->getProperties();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$propertyOne, $propertyTwo, $propertyThree], $actual->toArray());
     foreach ($actual as $property) {
         $this->assertContains($property, $attribute->getProperties());
     }
     // Add new option
     $this->assertSame($attribute, $attribute->addOption($optionThree));
     $actual = $attribute->getOptions();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$optionOne, $optionTwo, $optionThree], $actual->toArray());
     foreach ($actual as $option) {
         $this->assertContains($option, $attribute->getOptions());
     }
     // Add new default value
     $this->assertSame($attribute, $attribute->addDefaultValue($defaultValueThree));
     $actual = $attribute->getDefaultValues();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertEquals([$defaultValueOne, $defaultValueTwo, $defaultValueThree], $actual->toArray());
     foreach ($actual as $defaultValue) {
         $this->assertContains($defaultValue, $attribute->getDefaultValues());
     }
     // Remove label
     $this->assertSame($attribute, $attribute->removeLabel($labelOne));
     $actual = $attribute->getLabels();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertContains($labelTwo, $actual->toArray());
     $this->assertContains($labelThree, $actual->toArray());
     $this->assertNotContains($labelOne, $actual->toArray());
     // Remove property
     $this->assertSame($attribute, $attribute->removeProperty($propertyOne));
     $actual = $attribute->getProperties();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertContains($propertyTwo, $actual->toArray());
     $this->assertContains($propertyThree, $actual->toArray());
     $this->assertNotContains($propertyOne, $actual->toArray());
     // Remove option
     $this->assertSame($attribute, $attribute->removeOption($optionOne));
     $actual = $attribute->getOptions();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertContains($optionTwo, $actual->toArray());
     $this->assertContains($optionThree, $actual->toArray());
     $this->assertNotContains($optionOne, $actual->toArray());
     // Remove default value
     $this->assertSame($attribute, $attribute->removeDefaultValue($defaultValueOne));
     $actual = $attribute->getDefaultValues();
     $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $actual);
     $this->assertContains($defaultValueTwo, $actual->toArray());
     $this->assertContains($defaultValueThree, $actual->toArray());
     $this->assertNotContains($defaultValueOne, $actual->toArray());
 }