Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\Tests\telephone\Functional;
Dries Buytaert
committed
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
Dries Buytaert
committed
/**
* Tests the creation of telephone fields.
*
* @group telephone
Dries Buytaert
committed
*/
class TelephoneFieldTest extends BrowserTestBase {
Dries Buytaert
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
Dries Buytaert
committed
'field',
'node',
'telephone',
];
Dries Buytaert
committed
Alex Pott
committed
/**
* A user with permission to create articles.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
Dries Buytaert
committed
Alex Pott
committed
protected function setUp() {
Dries Buytaert
committed
parent::setUp();
$this->drupalCreateContentType(['type' => 'article']);
$this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
Alex Pott
committed
$this->drupalLogin($this->webUser);
Dries Buytaert
committed
}
// Test fields.
/**
* Helper function for testTelephoneField().
*/
public function testTelephoneField() {
Dries Buytaert
committed
// Add the telephone field to the article content type.
FieldStorageConfig::create([
Alex Pott
committed
'field_name' => 'field_telephone',
'entity_type' => 'node',
Dries Buytaert
committed
'type' => 'telephone',
])->save();
FieldConfig::create([
Dries Buytaert
committed
'field_name' => 'field_telephone',
'label' => 'Telephone Number',
'entity_type' => 'node',
'bundle' => 'article',
])->save();
Alex Pott
committed
entity_get_form_display('node', 'article', 'default')
->setComponent('field_telephone', [
catch
committed
'type' => 'telephone_default',
'settings' => [
catch
committed
'placeholder' => '123-456-7890',
],
])
Alex Pott
committed
->save();
Dries Buytaert
committed
entity_get_display('node', 'article', 'default')
->setComponent('field_telephone', [
Dries Buytaert
committed
'type' => 'telephone_link',
'weight' => 1,
])
Dries Buytaert
committed
->save();
catch
committed
// Display creation form.
$this->drupalGet('node/add/article');
Angie Byron
committed
$this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
catch
committed
$this->assertRaw('placeholder="123-456-7890"');
Alex Pott
committed
// Test basic entry of telephone field.
$edit = [
Alex Pott
committed
'title[0][value]' => $this->randomMachineName(),
'field_telephone[0][value]' => "123456789",
];
Dries Buytaert
committed
$this->drupalPostForm(NULL, $edit, t('Save'));
Dries Buytaert
committed
$this->assertRaw('<a href="tel:123456789">', 'A telephone link is provided on the article node page.');
catch
committed
// Add number with a space in it. Need to ensure it is stripped on output.
$edit = [
Alex Pott
committed
'title[0][value]' => $this->randomMachineName(),
'field_telephone[0][value]' => "1234 56789",
];
Dries Buytaert
committed
$this->drupalPostForm('node/add/article', $edit, t('Save'));
Dries Buytaert
committed
$this->assertRaw('<a href="tel:123456789">', 'Telephone link is output with whitespace removed.');
}