Version 1
[yaffs-website] / web / core / modules / aggregator / tests / src / Kernel / AggregatorTitleTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Kernel;
4
5 use Drupal\aggregator\Entity\Feed;
6 use Drupal\aggregator\Entity\Item;
7 use Drupal\KernelTests\KernelTestBase;
8
9
10 /**
11  * Tests the aggregator_title formatter.
12  *
13  * @group field
14  */
15 class AggregatorTitleTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['file', 'field', 'options', 'aggregator', 'system'];
23
24   /**
25    * The field name that is tested.
26    *
27    * @var string
28    */
29   protected $fieldName;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     $this->installConfig(['field']);
38     $this->installEntitySchema('aggregator_feed');
39     $this->installEntitySchema('aggregator_item');
40
41     \Drupal::service('router.builder')->rebuild();
42
43     $this->fieldName = 'title';
44   }
45
46   /**
47    * Tests the formatter output.
48    */
49   public function testStringFormatter() {
50     // Create an aggregator feed.
51     $aggregator_feed = Feed::create([
52       'title' => 'testing title',
53       'url' => 'http://www.example.com',
54     ]);
55     $aggregator_feed->save();
56
57     // Create an aggregator feed item.
58     $aggregator_item = Item::create([
59       'title' => 'test title',
60       'fid' => $aggregator_feed->id(),
61       'link' => 'http://www.example.com',
62       ]);
63     $aggregator_item->save();
64
65     // Verify aggregator feed title with and without links.
66     $build = $aggregator_feed->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => TRUE]]);
67     $result = $this->render($build);
68
69     $this->assertContains('testing title', $result);
70     $this->assertContains('href="' . $aggregator_feed->getUrl() . '"', $result);
71
72     $build = $aggregator_feed->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => FALSE]]);
73     $result = $this->render($build);
74     $this->assertContains('testing title', $result);
75     $this->assertNotContains($aggregator_feed->getUrl(), $result);
76
77     // Verify aggregator item title with and without links.
78     $build = $aggregator_item->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => TRUE]]);
79     $result = $this->render($build);
80
81     $this->assertContains('test title', $result);
82     $this->assertContains('href="' . $aggregator_item->getLink() . '"', $result);
83
84     $build = $aggregator_item->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => FALSE]]);
85     $result = $this->render($build);
86     $this->assertContains('test title', $result);
87     $this->assertNotContains($aggregator_item->getLink(), $result);
88   }
89
90 }