-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests for User #8
Open
agr0chal
wants to merge
10
commits into
drupalsocialinitiative:8.x-2.x
Choose a base branch
from
agr0chal:agrochal
base: 8.x-2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8cffc0b
Bug fix in UserManager.php and tests for it
agr0chal e862a67
Update SocialPostUserTest.php
agr0chal d21bb8c
Created tests for getAccountsByUserId and checkIfUserExists
agr0chal 4c8c26e
Update SocialPostUserTest.php
agr0chal 2904cc8
Proposal
agr0chal f2963de
try catch construction in addRecord function and test for it
agr0chal 83654be
Changed catching exception and added setup for addRecord
agr0chal cf4a622
Minor change
agr0chal f1d666f
Changes requested in comments
agr0chal 2c4c146
Update SocialPostUserTest.php
agr0chal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\social_post\Unit; | ||
|
||
use Drupal\Core\Entity\EntityStorageInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
use Drupal\Core\Session\AccountProxy; | ||
use Drupal\social_post\Entity\SocialPost; | ||
use Drupal\social_post\SocialPostDataHandler; | ||
use Drupal\social_post\User\UserManager; | ||
use Drupal\Tests\UnitTestCase; | ||
|
||
/** | ||
* Tests social_post User. | ||
* | ||
* @group social_post | ||
*/ | ||
class SocialPostUserTest extends UnitTestCase { | ||
|
||
/** | ||
* The tested Social Post UserManager. | ||
* | ||
* @var \Drupal\social_post\User\UserManager | ||
*/ | ||
protected $userManager; | ||
|
||
/** | ||
* The mocked Social Post Data Handler. | ||
* | ||
* @var \Drupal\social_post\SocialPostDataHandler | ||
*/ | ||
protected $dataHandler; | ||
|
||
/** | ||
* The mocked Entity Type Manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
/** | ||
* The mocked array of the session keys. | ||
* | ||
* @var array | ||
*/ | ||
protected $sessionKeys; | ||
|
||
/** | ||
* The test plugin id. | ||
* | ||
* @var string | ||
*/ | ||
protected $pluginId = 'social_post_test'; | ||
|
||
/** | ||
* The test user id. | ||
* | ||
* @var int | ||
*/ | ||
protected $userId = 21353; | ||
|
||
/** | ||
* The test provider user id. | ||
* | ||
* @var string | ||
*/ | ||
protected $providerUserId = 'some_id'; | ||
|
||
/** | ||
* The mocked Social Post. | ||
* | ||
* @var \Drupal\social_post\Entity\SocialPost | ||
*/ | ||
protected $socialPost; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp() { | ||
$current_user = $this->createMock(AccountProxy::class); | ||
$this->userStorage = $this->createMock(EntityStorageInterface::class); | ||
|
||
$this->socialPost = $this->createMock(SocialPost::class); | ||
|
||
$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); | ||
$this->entityTypeManager->expects($this->any()) | ||
->method('getStorage') | ||
->with('social_post') | ||
->will($this->returnValue($this->userStorage)); | ||
|
||
$logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); | ||
|
||
$this->dataHandler = $this->getMockBuilder(SocialPostDataHandler::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['get', 'set', 'getSessionPrefix']) | ||
->getMock(); | ||
|
||
$this->sessionKeys = []; | ||
|
||
$this->userManager = new UserManager($this->entityTypeManager, $current_user, $this->dataHandler, $logger_factory); | ||
|
||
} | ||
|
||
/** | ||
* @covers Drupal\social_post\User\UserManager::setPluginId | ||
*/ | ||
public function testSetPluginId() { | ||
$this->assertEquals(NULL, $this->userManager->getPluginId()); | ||
$this->userManager->setPluginId('social_post_test'); | ||
$this->assertEquals('social_post_test', $this->userManager->getPluginId()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_post\User\UserManager::getPluginId | ||
*/ | ||
public function testGetPluginId() { | ||
$this->userManager->setPluginId('social_post_test2'); | ||
$this->assertEquals('social_post_test2', $this->userManager->getPluginId()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_post\User\UserManager::getSessionKeys | ||
*/ | ||
public function testGetSessionKeys() { | ||
$sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3', 'pL2bxA2xz' => 'l2AYxbA9a']; | ||
|
||
$this->userManager->setSessionKeysToNullify(array_keys($sample_session)); | ||
$this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_post\User\UserManager::setSessionKeysToNullify | ||
*/ | ||
public function testSetSessionKeysToNullify() { | ||
$sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3', 'pL2bxA2xz' => 'l2AYxbA9a']; | ||
|
||
$this->assertNotEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); | ||
$this->userManager->setSessionKeysToNullify(array_keys($sample_session)); | ||
$this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_post\User\UserManager::nullifySessionKeys | ||
*/ | ||
public function testNullifySessionKeys() { | ||
$sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3']; | ||
|
||
$this->dataHandler->expects($this->any()) | ||
->method('getSessionPrefix') | ||
->will($this->returnCallback(function () { | ||
return 'xB2g22_'; | ||
})); | ||
|
||
$this->dataHandler->expects($this->any()) | ||
->method('get') | ||
->with($this->isType('string')) | ||
->will($this->returnCallback(function ($key) { | ||
return $this->sessionKeys[$this->dataHandler->getSessionPrefix() . $key]; | ||
})); | ||
|
||
$this->dataHandler->expects($this->any()) | ||
->method('set') | ||
->with($this->isType('string'), $this->anything()) | ||
->will($this->returnCallback(function ($key, $value) { | ||
$this->sessionKeys[$this->dataHandler->getSessionPrefix() . $key] = $value; | ||
})); | ||
|
||
$this->dataHandler->set('x1Sn2lPZZ', 'ikSn2AZj3'); | ||
$this->assertEquals('ikSn2AZj3', $this->dataHandler->get('x1Sn2lPZZ')); | ||
|
||
$this->userManager->setSessionKeysToNullify(array_keys($sample_session)); | ||
$this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); | ||
|
||
$this->userManager->nullifySessionKeys(); | ||
|
||
$this->assertEquals(NULL, $this->dataHandler->get('x1Sn2lPZZ')); | ||
} | ||
|
||
/** | ||
* Tests the checkIfUserExists method with no account returned. | ||
* | ||
* @covers Drupal\social_post\User\UserManager::checkIfUserExists | ||
*/ | ||
public function testCheckIfUserExistsWithNoUserReturned() { | ||
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) | ||
->will($this->returnValue([])); | ||
$this->userManager->setPluginId($this->pluginId); | ||
$this->assertFalse($this->userManager->checkIfUserExists($this->providerUserId)); | ||
} | ||
|
||
/** | ||
* Tests the checkIfUserExists method with account returned. | ||
* | ||
* @covers Drupal\social_post\User\UserManager::checkIfUserExists | ||
*/ | ||
public function testCheckIfUserExistsWithUserReturned() { | ||
$this->socialPost->expects($this->any()) | ||
->method('getUserId') | ||
->will($this->returnValue(97212)); | ||
|
||
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) | ||
->will($this->returnValue([$this->socialPost])); | ||
|
||
$this->userManager->setPluginId($this->pluginId); | ||
$this->assertEquals(97212, $this->userManager->checkIfUserExists($this->providerUserId)); | ||
} | ||
|
||
/** | ||
* Tests the getAccountsByUserId method with no account returned. | ||
* | ||
* @covers Drupal\social_post\User\UserManager::getAccountsByUserId | ||
*/ | ||
public function testGetAccountsByUserIdWithNoAccountReturned() { | ||
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) | ||
->will($this->returnValue([])); | ||
|
||
$this->userManager->setPluginId($this->pluginId); | ||
$this->assertEquals([], $this->userManager->getAccountsByUserId($this->userId)); | ||
} | ||
|
||
/** | ||
* Tests the getAccountsByUserId method with account returned. | ||
* | ||
* @covers Drupal\social_post\User\UserManager::getAccountsByUserId | ||
*/ | ||
public function testGetAccountsByUserIdWithAccountReturned() { | ||
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) | ||
->will($this->returnValue(['test'])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return an instance of |
||
|
||
$this->userManager->setPluginId($this->pluginId); | ||
$this->assertEquals(['test'], $this->userManager->getAccountsByUserId($this->userId)); | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No tests for other methods such as addRecord, getToken, etc? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it
@var \Drupal\social_post\User\UserManager|\PHPUnit_Framework_MockObject_MockObject
, so editors can pick up theexpects
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was not addressed