コード例 #1
0
 public function testStringFormatter()
 {
     // Create an aggregator feed.
     $aggregator_feed = Feed::create(['title' => 'testing title', 'url' => 'http://www.example.com']);
     $aggregator_feed->save();
     // Create an aggregator feed item.
     $aggregator_item = Item::create(['title' => 'test title', 'fid' => $aggregator_feed->id(), 'link' => 'http://www.example.com']);
     $aggregator_item->save();
     // Verify aggregator feed title with and without links.
     $build = $aggregator_feed->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => TRUE]]);
     $result = $this->render($build);
     $this->assertTrue(strpos($result, 'testing title'));
     $this->assertTrue(strpos($result, 'href="' . $aggregator_feed->getUrl()) . '"');
     $build = $aggregator_feed->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => FALSE]]);
     $result = $this->render($build);
     $this->assertTrue(strpos($result, 'testing title') === 0);
     $this->assertTrue(strpos($result, $aggregator_feed->getUrl()) === FALSE);
     // Verify aggregator item title with and without links.
     $build = $aggregator_item->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => TRUE]]);
     $result = $this->render($build);
     $this->assertTrue(strpos($result, 'test title'));
     $this->assertTrue(strpos($result, 'href="' . $aggregator_item->getLink()) . '"');
     $build = $aggregator_item->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => FALSE]]);
     $result = $this->render($build);
     $this->assertTrue(strpos($result, 'test title') === 0);
     $this->assertTrue(strpos($result, $aggregator_item->getLink()) === FALSE);
 }
コード例 #2
0
 /**
  * Checks access for aggregator_item fields.
  */
 public function testAggregatorItemFields()
 {
     $feed = Feed::create(['title' => 'Drupal org', 'url' => 'https://www.drupal.org/rss.xml']);
     $feed->save();
     $item = Item::create(['title' => 'Test title', 'fid' => $feed->id(), 'description' => 'Test description']);
     $item->save();
     // @todo Expand the test coverage in https://www.drupal.org/node/2464635
     $this->assertFieldAccess('aggregator_item', 'title', $item->getTitle());
     $this->assertFieldAccess('aggregator_item', 'langcode', $item->language()->getName());
     $this->assertFieldAccess('aggregator_item', 'description', $item->getDescription());
 }
コード例 #3
0
 /**
  * Tests that when creating a feed item, the feed tag is invalidated.
  */
 public function testEntityCreation()
 {
     // Create a cache entry that is tagged with a feed cache tag.
     \Drupal::cache('render')->set('foo', 'bar', \Drupal\Core\Cache\CacheBackendInterface::CACHE_PERMANENT, $this->entity->getCacheTag());
     // Verify a cache hit.
     $this->verifyRenderCache('foo', array('aggregator_feed:1'));
     // Now create a feed item in that feed.
     Item::create(array('fid' => $this->entity->getFeedId(), 'title' => t('Llama 2'), 'path' => 'https://groups.drupal.org/'))->save();
     // Verify a cache miss.
     $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new feed item invalidates the cache tag of the feed.');
 }
コード例 #4
0
 /**
  * Tests attempting to create a feed item without a feed.
  */
 public function testEntityCreation()
 {
     $entity = Item::create(['title' => t('Llama 2'), 'path' => 'https://groups.drupal.org/']);
     $violations = $entity->validate();
     $this->assertCount(1, $violations);
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function process(FeedInterface $feed)
 {
     if (!is_array($feed->items)) {
         return;
     }
     foreach ($feed->items as $item) {
         // @todo: The default entity view builder always returns an empty
         //   array, which is ignored in aggregator_save_item() currently. Should
         //   probably be fixed.
         if (empty($item['title'])) {
             continue;
         }
         // Save this item. Try to avoid duplicate entries as much as possible. If
         // we find a duplicate entry, we resolve it and pass along its ID is such
         // that we can update it if needed.
         if (!empty($item['guid'])) {
             $values = array('fid' => $feed->id(), 'guid' => $item['guid']);
         } elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) {
             $values = array('fid' => $feed->id(), 'link' => $item['link']);
         } else {
             $values = array('fid' => $feed->id(), 'title' => $item['title']);
         }
         // Try to load an existing entry.
         if ($entry = entity_load_multiple_by_properties('aggregator_item', $values)) {
             $entry = reset($entry);
         } else {
             $entry = Item::create(array('langcode' => $feed->language()->getId()));
         }
         if ($item['timestamp']) {
             $entry->setPostedTime($item['timestamp']);
         }
         // Make sure the item title and author fit in the 255 varchar column.
         $entry->setTitle(Unicode::truncate($item['title'], 255, TRUE, TRUE));
         $entry->setAuthor(Unicode::truncate($item['author'], 255, TRUE, TRUE));
         $entry->setFeedId($feed->id());
         $entry->setLink($item['link']);
         $entry->setGuid($item['guid']);
         $description = '';
         if (!empty($item['description'])) {
             $description = $item['description'];
         }
         $entry->setDescription($description);
         $entry->save();
     }
 }