Пример #1
0
 public function testMultipleSelectValues()
 {
     // create a multiple value select attribute
     $multiSel = SpecField::getNewInstance($this->productCategory, SpecField::DATATYPE_NUMBERS, SpecField::TYPE_NUMBERS_SELECTOR);
     $multiSel->isMultiValue->set(true);
     $multiSel->setValueByLang('name', 'en', 'Select multiple values');
     $multiSel->setValueByLang('name', 'lt', 'Pasirinkite kelias reiksmes');
     $multiSel->save();
     $values = array();
     for ($k = 0; $k < 5; $k++) {
         $inst = SpecFieldValue::getNewInstance($multiSel);
         $inst->setValueByLang('value', 'en', $k);
         $inst->setValueByLang('value', 'lt', 'Blaah');
         $inst->save();
         $values[] = $inst;
     }
     // assign the multiselect values
     $this->product->setAttributeValue($multiSel, $values[1]);
     $this->product->setAttributeValue($multiSel, $values[3]);
     $this->product->save();
     $array = $this->product->toArray();
     $this->assertEqual(2, count($array['attributes'][$multiSel->getID()]['values']));
     // assign one more multiselect value
     $this->product->setAttributeValue($multiSel, $values[2]);
     $this->product->save();
     $array = $this->product->toArray();
     $this->assertEqual(3, count($array['attributes'][$multiSel->getID()]['values']));
     // remove the first multiselect value
     $this->product->removeAttributeValue($multiSel, $values[1]);
     $this->product->save();
     $array = $this->product->toArray();
     $this->assertEqual(2, count($array['attributes'][$multiSel->getID()]['values']));
     // check for the number of SpecificationItem instances matching this field/product in database
     $query = 'SELECT COUNT(*) FROM SpecificationItem WHERE productID=' . $this->product->getID() . ' AND specFieldID=' . $multiSel->getID();
     $data = ActiveRecord::getDataBySQL($query);
     $this->assertEqual(2, array_shift(array_shift($data)));
     // remove the multiselect value altogether
     $this->product->removeAttribute($multiSel);
     $this->product->save();
     // check for the number of SpecificationItem instances matching this field/product in database.
     // shouldn't be any after the value removal
     $query = 'SELECT COUNT(*) FROM SpecificationItem WHERE productID=' . $this->product->getID() . ' AND specFieldID=' . $multiSel->getID();
     $data = ActiveRecord::getDataBySQL($query);
     $this->assertEqual(0, array_shift(array_shift($data)));
     // set the values back, so we could test how the data is restored from DB
     $this->product->setAttributeValue($multiSel, $values[1]);
     $this->product->setAttributeValue($multiSel, $values[2]);
     $this->product->save();
 }
Пример #2
0
 public function testUpdateSpecificationItems()
 {
     $specFieldValues = array();
     foreach (range(1, 3) as $i) {
         $specFieldValues[$i] = SpecFieldValue::getNewInstance($this->specField);
         $specFieldValues[$i]->save();
     }
     $specificationItems = array();
     foreach (range(1, 2) as $i) {
         $specificationItems[$i] = SpecificationItem::getNewInstance($this->product, $this->specField, $specFieldValues[$i]);
         $specificationItems[$i]->save();
     }
     $specFieldValues[1]->mergeWith($specFieldValues[2]);
     $specFieldValues[1]->save();
     try {
         $specificationItems[1]->reload();
         $this->pass();
     } catch (ARNotFoundException $e) {
         $this->fail('The value into which other values are beind merged should be left alone');
     }
     try {
         $specificationItems[2]->reload();
         $this->fail('Merged value should be deleted');
     } catch (ARNotFoundException $e) {
         $this->pass();
     }
     // After merging values specification item should point to other value
     $this->assertTrue($specificationItems[1]->specFieldValue->get() === $specFieldValues[1]);
     $this->assertTrue($specificationItems[2]->specFieldValue->get() === $specFieldValues[2]);
     $this->assertTrue($specificationItems[2]->specFieldValue->get() !== $specFieldValues[3]);
 }