Example #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();
 }