View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\Tests\sms\Functional\SmsFrameworkTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\sms\Entity\SmsMessage;
use Drupal\Core\Render\RenderContext;
use Drupal\sms\Direction;
class SmsFrameworkViewsTest extends ViewsKernelTestBase {
use SmsFrameworkTestTrait;
public static $modules = [
'user',
'sms',
'sms_test_gateway',
'sms_test_views',
'telephone',
'dynamic_entity_reference',
'field',
];
public static $testViews = [
'sms_messages',
];
protected $smsProvider;
protected $gateway;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->installEntitySchema('user');
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_result');
$this
->installEntitySchema('sms_report');
$this->smsProvider = $this->container
->get('sms.provider');
$this->gateway = $this
->createMemoryGateway();
$this
->setFallbackGateway($this->gateway);
ViewTestData::createTestViews(get_class($this), [
'sms_test_views',
]);
}
public function testSms() {
$role = Role::create([
'id' => $this
->randomMachineName(),
]);
$role
->grantPermission('access user profiles');
$role
->grantPermission('administer smsframework');
$role
->save();
$user0 = User::create([
'name' => $this
->randomMachineName(),
]);
$user0
->addRole($role
->id());
$user0
->save();
$this->container
->get('current_user')
->setAccount($user0);
$user1 = User::create([
'name' => $this
->randomMachineName(),
]);
$user1
->save();
$user2 = User::create([
'name' => $this
->randomMachineName(),
]);
$message1 = SmsMessage::create([
'created' => 892818493,
]);
$message1
->setSenderEntity($user1)
->addRecipients($this
->randomPhoneNumbers(2))
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomMachineName())
->setSenderNumber('+123123123')
->setQueued(TRUE);
$this->smsProvider
->queue($message1);
$message2 = SmsMessage::create([
'created' => 499488211,
]);
$message2
->setRecipientEntity($user1)
->setSenderEntity($user2)
->addRecipients($this
->randomPhoneNumbers(1))
->setDirection(Direction::INCOMING)
->setMessage($this
->randomMachineName())
->setAutomated(FALSE)
->setProcessedTime(499488280)
->setGateway($this->gateway);
$message2
->setResult($this
->createMessageResult($message2));
$this->smsProvider
->queue($message2);
Views::viewsData()
->clear();
$view = Views::getView('sms_messages');
$view
->setDisplay('default');
$this
->executeView($view);
$this
->assertEquals(2, $view->total_rows);
$cols = [
'direction_1',
'sender_phone_number',
'recipient_phone_number',
'message',
'created',
'gateway',
'sender_entity__target_id',
'recipient_entity__target_id',
'automated',
'processed',
'queued',
];
$this
->assertEquals($cols, array_keys($view->field));
$renderer = \Drupal::service('renderer');
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['direction_1']
->advancedRender($view->result[0]);
});
$this
->assertEquals('Outgoing', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['direction_1']
->advancedRender($view->result[1]);
});
$this
->assertEquals('Incoming', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['sender_phone_number']
->advancedRender($view->result[0]);
});
$this
->assertEquals($message1
->getSenderNumber(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['sender_phone_number']
->advancedRender($view->result[1]);
});
$this
->assertEquals('', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['recipient_phone_number']
->advancedRender($view->result[0]);
});
$number1 = $message1
->getRecipients()[0];
$number2 = $message1
->getRecipients()[1];
$this
->assertEquals('<a href="tel:' . urlencode($number1) . '">' . $number1 . '</a>, <a href="tel:' . urlencode($number2) . '">' . $number2 . '</a>', $render);
$number1 = $message2
->getRecipients()[0];
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['recipient_phone_number']
->advancedRender($view->result[1]);
});
$this
->assertEquals('<a href="tel:' . urlencode($number1) . '">' . $number1 . '</a>', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['message']
->advancedRender($view->result[0]);
});
$this
->assertEquals($message1
->getMessage(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['message']
->advancedRender($view->result[1]);
});
$this
->assertEquals($message2
->getMessage(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['created']
->advancedRender($view->result[0]);
});
$this
->assertEquals('Fri, 04/17/1998 - 23:08', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['created']
->advancedRender($view->result[1]);
});
$this
->assertEquals('Wed, 10/30/1985 - 13:43', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['gateway']
->advancedRender($view->result[0]);
});
$this
->assertEquals($this->gateway
->toLink(NULL, 'edit-form')
->toString(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['gateway']
->advancedRender($view->result[1]);
});
$this
->assertEquals($this->gateway
->toLink(NULL, 'edit-form')
->toString(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['sender_entity__target_id']
->advancedRender($view->result[0]);
});
$this
->assertEquals($user1
->toLink()
->toString(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['sender_entity__target_id']
->advancedRender($view->result[1]);
});
$this
->assertEquals($user2
->toLink()
->toString(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['recipient_entity__target_id']
->advancedRender($view->result[0]);
});
$this
->assertEquals('None', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['recipient_entity__target_id']
->advancedRender($view->result[1]);
});
$this
->assertEquals($user1
->toLink()
->toString(), $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['automated']
->advancedRender($view->result[0]);
});
$this
->assertEquals('Automated', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['automated']
->advancedRender($view->result[1]);
});
$this
->assertEquals('Not automated', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['processed']
->advancedRender($view->result[0]);
});
$this
->assertEquals('', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['processed']
->advancedRender($view->result[1]);
});
$this
->assertEquals('Wed, 10/30/1985 - 13:44', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message1) {
return $view->field['queued']
->advancedRender($view->result[0]);
});
$this
->assertEquals('Queued', $render);
$render = $renderer
->executeInRenderContext(new RenderContext(), function () use ($view, $message2) {
return $view->field['queued']
->advancedRender($view->result[1]);
});
$this
->assertEquals('Not queued', $render);
}
}