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