/** * @test */ public function canGetPromotionContentDOM() { $content = '<?xml version="1.0" encoding="UTF-8"?><promotion> <title>test</title> <type>organic</type> <image>http://www.foobar.de/test.gif</image> <searchterms> <searchterm>one</searchterm> <searchterm>two</searchterm> </searchterms> <solrfieldvalues> <solrfieldvalue fieldname="test">Fooobar</solrfieldvalue> </solrfieldvalues> <content><![CDATA[ <html> <head></head> <body><![CDATA[test]]]]><![CDATA[></body> </html> ]]></content> </promotion>'; $this->promotion->__setProperty('content', $content); $this->assertInstanceOf('\\DOMDocument', $this->promotion->getPromotionContentDOM(), 'Could not dom object from content'); $xpath = new \DOMXPath($this->promotion->getPromotionContentDOM()); $typePath = $xpath->query("//body"); $this->assertEquals(trim((string) $typePath->item(0)->textContent), "test", "Could not get body content from promotion content"); }
/** * @param Promotion $promotion * @param string $contentXML * @throws \Searchperience\Common\Exception\InvalidArgumentException; */ public function fromXML(Promotion $promotion, $contentXML) { $contentDOM = new \DOMDocument('1.0', 'UTF-8'); $result = @$contentDOM->loadXML($contentXML); if ($result === false) { throw new InvalidArgumentException("No xml content: " . $contentXML); } $xpath = new \DOMXPath($contentDOM); $promotion->__setProperty('promotionTitle', $this->getFirstNodeContent($xpath, '//title')); $promotion->__setProperty('promotionType', $this->getFirstNodeContent($xpath, '//type')); $promotion->__setProperty('imageUrl', $this->getFirstNodeContent($xpath, '//image')); $promotion->__setProperty('language', $this->getFirstNodeContent($xpath, '//language')); $promotion->__setProperty('limitedTimeFrom', $this->getFirstNodeContent($xpath, '//limitedTimeFrom')); $promotion->__setProperty('limitedTimeTo', $this->getFirstNodeContent($xpath, '//limitedTimeTo')); $searchTerms = $xpath->query("//searchterm"); $keywords = array(); foreach ($searchTerms as $searchTerm) { $keywords[] = $searchTerm->textContent; } $promotion->__setProperty('keywords', $keywords); $solrFieldValues = $xpath->query("//solrfieldvalue"); $fieldValues = array(); foreach ($solrFieldValues as $solrFieldValue) { /** @var $solrFieldValue \DOMElement */ $fieldName = $solrFieldValue->getAttribute("fieldname"); $fieldValues[$fieldName] = $solrFieldValue->textContent; } $promotion->__setProperty('fieldValues', $fieldValues); $promotion->__setProperty('promotionContent', $this->getFirstNodeContent($xpath, '//content')); }