public function testToArrayValuesEqualToNative() { $img = new Image([Image::PROPERTY_LOCATION => 'some/other.png', Image::PROPERTY_TITLE => 'other_title']); $a = $img->toArray(); $b = $img->toNative(); $this->assertEquals($a, $b); }
public function testToNativeRoundtrip() { $img_data = [Image::PROPERTY_LOCATION => 'some.jpg', Image::PROPERTY_COPYRIGHT => 'some copyright string', Image::PROPERTY_AOI => '[12,123,42,542]', Image::PROPERTY_METADATA => ['foo' => 'bar', 'leet' => 1337, 'bool' => true]]; $native = [Image::PROPERTY_LOCATION => 'some.jpg', Image::PROPERTY_TITLE => '', Image::PROPERTY_CAPTION => '', Image::PROPERTY_COPYRIGHT => 'some copyright string', Image::PROPERTY_COPYRIGHT_URL => '', Image::PROPERTY_SOURCE => '', Image::PROPERTY_WIDTH => 0, Image::PROPERTY_HEIGHT => 0, Image::PROPERTY_FILESIZE => 0, Image::PROPERTY_FILENAME => '', Image::PROPERTY_MIMETYPE => '', Image::PROPERTY_AOI => '[12,123,42,542]', Image::PROPERTY_METADATA => ['foo' => 'bar', 'leet' => 1337, 'bool' => true]]; $attribute = new ImageAttribute('image', $this->getTypeMock()); $valueholder = $attribute->createValueHolder(); $valueholder->setValue(Image::createFromArray($img_data)); $this->assertInstanceOf(Image::CLASS, $valueholder->getValue()); $this->assertEquals($native, $valueholder->toNative()); $result = $valueholder->setValue($valueholder->toNative()); $this->assertEquals(IncidentInterface::SUCCESS, $result->getSeverity()); $this->assertInstanceOf(Image::CLASS, $valueholder->getValue()); $this->assertEquals($native, $valueholder->toNative()); }
public function testValueComparison() { $img_data = [Image::PROPERTY_LOCATION => 'some.jpg', Image::PROPERTY_COPYRIGHT => 'some copyright string', Image::PROPERTY_METADATA => ['foo' => 'bar', 'leet' => 1337, 'bool' => true]]; $img2_data = $img_data; $img2_data[Image::PROPERTY_SOURCE] = 'some source'; $img_list_data = [$img_data, $img2_data]; $expected_list = [Image::createFromArray($img_data), Image::createFromArray($img2_data)]; $img3_data = $img2_data; $img3_data[Image::PROPERTY_SOURCE] = 'some other source'; $expected_other_list = [Image::createFromArray($img_data), Image::createFromArray($img3_data)]; $attribute = new ImageListAttribute('imagelist', $this->getTypeMock()); $valueholder = $attribute->createValueHolder(); $valueholder->setValue($img_list_data); $this->assertInstanceOf(Image::CLASS, $valueholder->getValue()[0]); $this->assertInstanceOf(Image::CLASS, $valueholder->getValue()[1]); $this->assertTrue($valueholder->sameValueAs($expected_list)); $this->assertFalse($valueholder->sameValueAs($expected_other_list)); }
public function testMinimumImageListIsValid() { $rule = new ImageListRule('imagelist', []); $valid = $rule->apply([Image::createFromArray([Image::PROPERTY_LOCATION => 'asdf.jpg'])]); $this->assertTrue($valid); }
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; } $image = Image::createFromArray($value); } elseif ($value instanceof Image) { $image = Image::createFromArray($value->toNative()); } else { $this->throwError('invalid_type', ['value' => $value], IncidentInterface::CRITICAL); return false; } $incoming_data = $image->toNative(); $data = []; foreach ($this->validations as $property_name => $implementor) { $rule = new $implementor('valid-' . $property_name, $this->getSupportedOptionsFor($implementor, $property_name)); 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[Image::PROPERTY_METADATA])) { $this->throwIncidentsAsErrors($rule, Image::PROPERTY_METADATA); return false; } $data[Image::PROPERTY_METADATA] = $rule->getSanitizedValue(); // set the sanitized new image data $this->setSanitizedValue(Image::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; }
public function testImageWidthHeightConstraitsApply() { $rule = new ImageRule('image', [ImageRule::OPTION_WIDTH_MIN_VALUE => 320, ImageRule::OPTION_HEIGHT_MIN_VALUE => 200, ImageRule::OPTION_WIDTH_MAX_VALUE => 1920, ImageRule::OPTION_HEIGHT_MAX_VALUE => 1080]); $valid = $rule->apply(Image::createFromArray([Image::PROPERTY_LOCATION => 'asdf.jpg', Image::PROPERTY_WIDTH => 800, Image::PROPERTY_HEIGHT => 1600])); $this->assertFalse($valid); }