<?php include dirname(__FILE__) . '/../../../../test/bootstrap/unit.php'; require_once dirname(__FILE__) . '/../../lib/sfFeedEnclosure.class.php'; $t = new lime_test(4, new lime_output_color()); $enclosureParams = array('url' => 'foo.com', 'length' => '1234', 'mimeType' => 'foobarmimetype'); $enclosure = new sfFeedEnclosure(); $t->isa_ok($enclosure->initialize($enclosureParams), 'sfFeedEnclosure', 'initialize() returns the current feed enclosure object'); $t->is($enclosure->getUrl(), $enclosureParams['url'], 'getUrl() gets the feed enclosure url'); $t->is($enclosure->getLength(), $enclosureParams['length'], 'getLength() gets the feed enclosure length'); $t->is($enclosure->getMimeType(), $enclosureParams['mimeType'], 'getMimeType() gets the feed enclosure mimetype');
/** * Populate the feed object from a XML feed string. * * @param string A XML feed (RSS 2.0 format) * * @return sfRss10Feed The current object * * @throws Exception If the argument is not a well-formatted RSS feed */ public function fromXml($feedXml) { preg_match('/^<\\?xml\\s*version="1\\.0"\\s*encoding="(.*?)".*?\\?>$/mi', $feedXml, $matches); if (isset($matches[1])) { $this->setEncoding($matches[1]); } $feedXml = simplexml_load_string($feedXml); if (!$feedXml) { throw new Exception('Error creating feed from XML: string is not well-formatted XML'); } $authorString = (string) $feedXml->channel[0]->managingEditor; $pos = strpos($authorString, '('); if ($pos !== false) { $this->setAuthorEmail(trim(substr($authorString, 0, $pos))); $this->setAuthorName(trim(substr($authorString, $pos + 1, strlen($authorString) - $pos - 2))); } else { $this->setAuthorEmail(trim($authorString)); } $this->setTitle((string) $feedXml->channel[0]->title); $this->setLink((string) $feedXml->channel[0]->link); $this->setDescription((string) $feedXml->channel[0]->description); $this->setLanguage((string) $feedXml->channel[0]->language); if ($feedXml->channel[0]->image) { $image = new sfFeedImage(array("image" => (string) $feedXml->channel[0]->image->url, "imageX" => (int) $feedXml->channel[0]->image->width, "imageY" => (int) $feedXml->channel[0]->image->height, "link" => (string) $feedXml->channel[0]->image->link, "title" => (string) $feedXml->channel[0]->image->title)); $this->setImage($image); } $categories = array(); foreach ($feedXml->channel[0]->category as $category) { $categories[] = (string) $category; } $this->setCategories($categories); foreach ($feedXml->channel[0]->item as $itemXml) { $url = (string) $itemXml->link; $authorString = (string) $itemXml->author; $pos = strpos($authorString, '('); if ($pos !== false) { $authorEmail = trim(substr($authorString, 0, $pos)); $authorName = trim(substr($authorString, $pos + 1, strlen($authorString) - $pos - 2)); } else { $authorEmail = trim($authorString); $authorName = ''; } $dc = $itemXml->children("http://purl.org/dc/elements/1.1/"); if (!$authorName) { $authorName = (string) $dc->creator; } $pubdate = strtotime(str_replace(array('UT', 'Z'), '', (string) $itemXml->pubDate)); if (!$pubdate) { if ((string) $dc->date) { $pubdate = strtotime(str_replace(array('UT', 'Z'), '', (string) $dc->date)); } else { if (preg_match('/\\d{4}\\/\\d{2}\\/\\d{2}/', $url, $matches)) { $pubdate = strtotime($matches[0]); } else { $pubdate = 0; } } } $content = $itemXml->children("http://purl.org/rss/1.0/modules/content/"); $categories = array(); foreach ($itemXml->category as $category) { $categories[] = (string) $category; } if ($enclosureElement = $itemXml->enclosure) { $enclosure = new sfFeedEnclosure(); $enclosure->setUrl((string) $enclosureElement['url']); $enclosure->setLength((string) $enclosureElement['length']); $enclosure->setMimeType((string) $enclosureElement['type']); } else { $enclosure = null; } $this->addItemFromArray(array('title' => (string) $itemXml->title, 'link' => $url, 'description' => (string) $itemXml->description, 'content' => (string) $content->encoded, 'authorName' => $authorName, 'authorEmail' => $authorEmail, 'pubDate' => $pubdate, 'comments' => (string) $itemXml->comments, 'uniqueId' => (string) $itemXml->guid, 'enclosure' => $enclosure, 'categories' => $categories, 'feed' => $this)); } }
<?php define('SF_ROOT_DIR', realpath(dirname(__FILE__) . '/../../../..')); define('SF_APP', 'frontend'); include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php'; $b = new sfTestBrowser(); $b->initialize(); $t = new lime_test(15, new lime_output_color()); $enclosureParams = array('url' => 'foo.com', 'length' => '1234', 'mimeType' => 'foobarmimetype'); $enclosure = new sfFeedEnclosure(); $enclosure->initialize($enclosureParams); $item_params = array('title' => 'foo', 'link' => 'http://www.example.com', 'description' => 'foobar baz', 'content' => 'hey, do you foo, bar?', 'authorName' => 'francois', 'authorEmail' => '*****@*****.**', 'authorLink' => 'http://francois.toto.com', 'pubDate' => '12345', 'comments' => 'this is foo bar baz', 'uniqueId' => 'hello world', 'enclosure' => $enclosure, 'categories' => array('foo', 'bar')); $item = new sfFeedItem(); $t->isa_ok($item->initialize($item_params), 'sfFeedItem', 'initialize() returns the current feed item object'); $t->is($item->getTitle(), $item_params['title'], 'getTitle() gets the item title'); $t->is($item->getLink(), $item_params['link'], 'getLink() gets the item link'); $t->is($item->getDescription(), $item_params['description'], 'getDescription() gets the item description'); $t->is($item->getContent(), $item_params['content'], 'getContent() gets the item content'); $t->is($item->getAuthorName(), $item_params['authorName'], 'getAuthorName() gets the item author name'); $t->is($item->getAuthorEmail(), $item_params['authorEmail'], 'getAuthorEmail() gets the item author email'); $t->is($item->getAuthorLink(), $item_params['authorLink'], 'getAuthorLink() gets the item author link'); $t->is($item->getPubDate(), $item_params['pubDate'], 'getPubDate() gets the item publication date'); $t->is($item->getComments(), $item_params['comments'], 'getComments() gets the item comments'); $t->is($item->getUniqueId(), $item_params['uniqueId'], 'getUniqueId() gets the item unique id'); $t->is($item->getEnclosure(), $item_params['enclosure'], 'getEnclosure() gets the item enclosure'); $t->is($item->getCategories(), $item_params['categories'], 'getCategories() gets the item categories'); $item_params = array('title' => 'foo', 'link' => 'http://www.example.com', 'content' => 'hey, do you <strong>foo</strong>, my dear bar?'); $item = new sfFeedItem(); $item->initialize($item_params); $t->is($item->getDescription(), strip_tags($item_params['content']), 'getDescription() gets the stripped item content when no description is defined'); sfConfig::set('app_feed_item_max_length', 5);
/** * Populate the feed object from a XML feed string. * * @param string A XML feed (Atom 1.0 format) * * @return sfAtom1Feed The current object * * @throws Exception If the argument is not a well-formatted Atom feed */ public function fromXml($feedXml) { preg_match('/^<\\?xml\\s*version="1\\.0"\\s*encoding="(.*?)".*?\\?>$/mi', $feedXml, $matches); if (isset($matches[1])) { $this->setEncoding($matches[1]); } $feedXml = simplexml_load_string($feedXml); if (!$feedXml) { throw new Exception('Error creating feed from XML: string is not well-formatted XML'); } $attributes = $feedXml->attributes('http://www.w3.org/XML/1998/namespace'); $this->setLanguage((string) $attributes['lang']); $this->setTitle((string) $feedXml->title); $feedXml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); if ($titles = $feedXml->xpath('atom:link[@rel="alternate"]')) { $this->setLink((string) $titles[0]['href']); } if ($titles = $feedXml->xpath('atom:link[@rel="self"]')) { $this->setFeedUrl((string) $titles[0]['href']); } $this->setSubtitle((string) $feedXml->subtitle); $this->setAuthorName((string) $feedXml->author->name); $this->setAuthorEmail((string) $feedXml->author->email); $this->setAuthorLink((string) $feedXml->author->uri); $image = new sfFeedImage(array("favicon" => (string) $feedXml->icon, "image" => (string) $feedXml->logo)); $this->setImage($image); $categories = array(); foreach ($feedXml->category as $category) { $categories[] = (string) $category['term']; } $this->setCategories($categories); foreach ($feedXml->entry as $itemXml) { $categories = array(); foreach ($itemXml->category as $category) { $categories[] = (string) $category['term']; } $url = (string) $itemXml->link['href']; $pubdate = strtotime((string) $itemXml->published); if (!$pubdate) { if ((string) $itemXml->updated) { $pubdate = strtotime((string) $itemXml->updated); } else { if ((string) $itemXml->modified) { $pubdate = strtotime((string) $itemXml->modified); } else { if (preg_match('/\\d{4}\\/\\d{2}\\/\\d{2}/', $url, $matches)) { $pubdate = strtotime($matches[0]); } else { $pubdate = 0; } } } } $itemXml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); if ($enclosures = $itemXml->xpath('atom:link[@rel="enclosure"]')) { $enclosure = new sfFeedEnclosure(); $enclosure->setUrl((string) $enclosures[0]['href']); $enclosure->setLength((string) $enclosures[0]['length']); $enclosure->setMimeType((string) $enclosures[0]['type']); } else { $enclosure = null; } $content = (string) $itemXml->content; if (!$content) { $content = preg_replace('/<\\/?content[^>]*>/iU', '', $itemXml->content->asXml()); } $this->addItemFromArray(array('title' => (string) $itemXml->title, 'link' => $url, 'authorName' => (string) $itemXml->author->name, 'authorEmail' => (string) $itemXml->author->email, 'authorLink' => (string) $itemXml->author->uri, 'pubDate' => $pubdate, 'description' => (string) $itemXml->summary, 'content' => $content, 'uniqueId' => (string) $itemXml->id, 'enclosure' => $enclosure, 'categories' => $categories, 'feed' => $this)); } return $this; }