Ejemplo n.º 1
0
 public function testValueComparison()
 {
     $asset_data = [Asset::PROPERTY_LOCATION => 'some.ods', Asset::PROPERTY_COPYRIGHT => 'some copyright string', Asset::PROPERTY_METADATA => ['foo' => 'bar', 'leet' => 1337, 'bool' => true]];
     $asset2_data = $asset_data;
     $asset2_data[Asset::PROPERTY_SOURCE] = 'some source';
     $asset_list_data = [$asset_data, $asset2_data];
     $expected_list = [Asset::createFromArray($asset_data), Asset::createFromArray($asset2_data)];
     $asset3_data = $asset2_data;
     $asset3_data[Asset::PROPERTY_SOURCE] = 'some other source';
     $expected_other_list = [Asset::createFromArray($asset_data), Asset::createFromArray($asset3_data)];
     $attribute = new AssetListAttribute('assetlist', $this->getTypeMock());
     $valueholder = $attribute->createValueHolder();
     $valueholder->setValue($asset_list_data);
     $this->assertInstanceOf(Asset::CLASS, $valueholder->getValue()[0]);
     $this->assertInstanceOf(Asset::CLASS, $valueholder->getValue()[1]);
     $this->assertTrue($valueholder->sameValueAs($expected_list));
     $this->assertFalse($valueholder->sameValueAs($expected_other_list));
 }
Ejemplo n.º 2
0
 public function testMinimumAssetIsValid()
 {
     $rule = new AssetRule('image', []);
     $valid = $rule->apply(Asset::createFromArray([Asset::PROPERTY_LOCATION => 'asdf.jpg']));
     $this->assertTrue($valid);
 }
Ejemplo n.º 3
0
 protected function execute($value, EntityInterface $entity = null)
 {
     try {
         if (is_array($value)) {
             if (!empty($value) && !$this->isAssoc($value)) {
                 $this->throwError('non_assoc_array', ['value' => $value], IncidentInterface::CRITICAL);
                 return false;
             }
             $asset = Asset::createFromArray($value);
         } elseif ($value instanceof Asset) {
             $asset = Asset::createFromArray($value->toNative());
         } else {
             $this->throwError('invalid_type', ['value' => $value], IncidentInterface::CRITICAL);
             return false;
         }
         $incoming_data = $asset->toNative();
         $data = [];
         foreach ($this->validations as $property_name => $implementor) {
             $rule_options = $this->getSupportedOptionsFor($implementor, $property_name);
             $rule = new $implementor('valid-' . $property_name, $rule_options);
             if (!$rule->apply($incoming_data[$property_name])) {
                 $this->throwIncidentsAsErrors($rule, $property_name);
                 return false;
             }
             $data[$property_name] = $rule->getSanitizedValue();
         }
         // meta data accepts scalar values
         $rule = new KeyValueListRule('valid-metadata', $this->getMetadataOptions());
         if (!$rule->apply($incoming_data[Asset::PROPERTY_METADATA])) {
             $this->throwIncidentsAsErrors($rule, Asset::PROPERTY_METADATA);
             return false;
         }
         $data[Asset::PROPERTY_METADATA] = $rule->getSanitizedValue();
         // set the sanitized new data
         $this->setSanitizedValue(Asset::createFromArray($data));
     } catch (Exception $e) {
         // pretty catch all, but there may be Assert and BadValueExceptions depending on usage / later changes
         $this->throwError('invalid_data', ['error' => $e->getMessage()], IncidentInterface::CRITICAL);
         return false;
     }
     return true;
 }
Ejemplo n.º 4
0
 public function testMinimumAssetListIsValid()
 {
     $rule = new AssetListRule('assetlist', []);
     $valid = $rule->apply([Asset::createFromArray([Asset::PROPERTY_LOCATION => 'asdf.ods'])]);
     $this->assertTrue($valid);
 }