Same name and namespace in other branches
  1. 8.x-1.x tests/src/FunctionalJavascript/EntityReferenceAjaxFormatterTest.php \Drupal\Tests\entity_reference_ajax_formatter\FunctionalJavascript\EntityReferenceAjaxFormatterTest 1 comment

Test the Entity Reference Ajax Formatter.

@group entity_reference_ajax_formatter

Hierarchy

  • class \Drupal\Tests\entity_reference_ajax_formatter\FunctionalJavascript\EntityReferenceAjaxFormatterTest extends \Drupal\FunctionalJavascriptTests\WebDriverTestBase

Expanded class hierarchy of EntityReferenceAjaxFormatterTest

File

tests/src/FunctionalJavascript/EntityReferenceAjaxFormatterTest.php, line 19

Namespace

Drupal\Tests\entity_reference_ajax_formatter\FunctionalJavascript
View source
class EntityReferenceAjaxFormatterTest extends WebDriverTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected $defaultTheme = 'stark';
    
    /**
     * The Entity View Display for the article node type.
     *
     * @var \Drupal\Core\Entity\Entity\EntityViewDisplay
     */
    protected EntityViewDisplayInterface $display;
    
    /**
     * The primary node to be testing.
     *
     * @var \Drupal\node\NodeInterface
     */
    protected NodeInterface $node;
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'node',
        'entity_reference_ajax_formatter',
    ];
    
    /**
     * Tests the behavior of the 'entity_reference_ajax_entity_view' formatter.
     */
    public function testFormatter() : void {
        $this->drupalGet("node/{$this->node->id()}");
        $session = $this->assertSession();
        $session->pageTextContains('Node #1');
        $session->pageTextNotContains('Node #2');
        $session->pageTextNotContains('Load More');
        // Test random sort.
        $this->display
            ->setComponent('field_ref', [
            'type' => 'entity_reference_ajax_entity_view',
            'settings' => [
                'number' => 3,
                'sort' => 1,
                'load_more' => TRUE,
                'max' => 8,
            ],
        ])
            ->save();
        $this->drupalGet("node/{$this->node->id()}");
        $page = $this->getSession()
            ->getPage();
        $this->assertSame(count($page->findAll('css', '.layout-content [role="article"] [role="article"]')), 3);
        $session->pageTextMatches('/Node #(\\d\\d|[^123])/');
        $session->pageTextContains('Load More');
        $page->clickLink('Load More');
        $session->assertWaitOnAjaxRequest();
        $this->assertSame(count($page->findAll('css', '.layout-content [role="article"] [role="article"]')), 6);
        $session->pageTextContains('Load More');
        $page->clickLink('Load More');
        $session->assertWaitOnAjaxRequest();
        $this->assertSame(count($page->findAll('css', '.layout-content [role="article"] [role="article"]')), 8);
        $session->pageTextNotContains('Load More');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->drupalCreateContentType([
            'type' => 'article',
        ]);
        $user = $this->drupalCreateUser([
            'create article content',
            'edit own article content',
        ]);
        $this->drupalLogin($user);
        $entityTypeManager = $this->container
            ->get('entity_type.manager');
        FieldStorageConfig::create([
            'field_name' => 'field_ref',
            'entity_type' => 'node',
            'type' => 'entity_reference',
            'cardinality' => '-1',
            'settings' => [
                'target_type' => 'node',
            ],
        ])->save();
        FieldConfig::create([
            'field_name' => 'field_ref',
            'label' => 'Node references',
            'entity_type' => 'node',
            'bundle' => 'article',
            'settings' => [
                'handler' => 'default:node',
                'handler_settings' => [
                    'target_bundles' => [
                        'article' => 'article',
                    ],
                    'sort' => [
                        'field' => '_none',
                    ],
                ],
            ],
        ])->save();
        $this->display = $entityTypeManager->getStorage('entity_view_display')
            ->load('node.article.default');
        $this->display
            ->setComponent('field_ref', [
            'type' => 'entity_reference_ajax_entity_view',
            'settings' => [
                'number' => 2,
            ],
        ])
            ->save();
        $nodes = [];
        $i = 0;
        while ($i <= 9) {
            $node = Node::create([
                'title' => "Node #{$i}",
                'type' => 'article',
            ]);
            $node->save();
            $nodes[] = [
                'target_id' => $node->id(),
            ];
            $i++;
        }
        $this->node = Node::create([
            'title' => 'Primary Node',
            'type' => 'article',
            'field_ref' => $nodes,
        ]);
        $this->node
            ->save();
    }

}

Members

Title Sort descending Modifiers Object type Summary
EntityReferenceAjaxFormatterTest::$defaultTheme protected property
EntityReferenceAjaxFormatterTest::$display protected property The Entity View Display for the article node type.
EntityReferenceAjaxFormatterTest::$modules protected static property
EntityReferenceAjaxFormatterTest::$node protected property The primary node to be testing.
EntityReferenceAjaxFormatterTest::setUp protected function
EntityReferenceAjaxFormatterTest::testFormatter public function Tests the behavior of the 'entity_reference_ajax_entity_view' formatter.