Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Update unit tests and remove deprecations #85

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
# stable version.
#
# Uncomment and set the Dev-Tools's commit value and commit this change.
# export GH_COMMIT=COMMIT_SHA
export GH_COMMIT=8d73b8a8a92856b78f7f66701e76e5e2e7be580d

bash <(curl -L https://raw.githubusercontent.com/dpc-sdp/dev-tools/master/install?"$(date +%s)") "$@"
21 changes: 7 additions & 14 deletions src/AliasStorageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ public function createSiteAliases(PathAliasInterface $path, NodeInterface $node
if (!$node) {
$node = $this->getNodeFromPathEntity($path);
}
/** @var \Drupal\Core\Entity\EntityStorageInterface $path_storage */
$path_storage = $this->entityTypeManager->getStorage('path_alias');
if ($node) {
/** @var string[] $aliases */
$aliases = $this->getAllSiteAliases($path, $node);
Expand All @@ -172,7 +170,7 @@ public function createSiteAliases(PathAliasInterface $path, NodeInterface $node
if ($existing_path->getPath() != $path->getPath()) {
$this->uniquify($alias, $path->language()->getId());
if ($original_alias != $alias) {
$path_storage->create([
$this->entityTypeManager->getStorage('path_alias')->create([
'path' => $path->getPath(),
'alias' => $alias,
'langcode' => $path->language()->getId(),
Expand All @@ -181,7 +179,7 @@ public function createSiteAliases(PathAliasInterface $path, NodeInterface $node
}
}
else {
$path_storage->create([
$this->entityTypeManager->getStorage('path_alias')->create([
'path' => $path->getPath(),
'alias' => $alias,
'langcode' => $path->language()->getId(),
Expand Down Expand Up @@ -222,12 +220,10 @@ public function updateSiteAliases($path, $original_path) {
if (!$old_path) {
$is_new = TRUE;
}
/** @var \Drupal\Core\Entity\EntityStorageInterface $path_storage */
$path_storage = $this->entityTypeManager->getStorage('path_alias');
try {
if ($is_new) {
$this->uniquify($alias, $path->language()->getId());
$path_storage->create([
$this->entityTypeManager->getStorage('path_alias')->create([
'path' => $path->getPath(),
'alias' => $alias,
'langcode' => $path->language()->getId(),
Expand Down Expand Up @@ -286,8 +282,7 @@ public function isAliasExists($alias, $langcode = '') {
if ($langcode) {
$conditions['langcode'] = $langcode;
}
$path_storage = $this->entityTypeManager->getStorage('path_alias');
$path = $path_storage->loadByProperties($conditions);
$path = $this->entityTypeManager->getStorage('path_alias')->loadByProperties($conditions);
return reset($path) ?: FALSE;
}

Expand Down Expand Up @@ -336,8 +331,7 @@ public function uniquify(&$alias, $langcode) {
* - langcode (string): The language code of the alias.
*/
public function loadAll(array $conditions) {
$path_storage = $this->entityTypeManager->getStorage('path_alias');
$paths = $path_storage->loadByProperties($conditions);
$paths = $this->entityTypeManager->getStorage('path_alias')->loadByProperties($conditions);
if (!$paths) {
return FALSE;
}
Expand All @@ -351,9 +345,8 @@ public function loadAll(array $conditions) {
* The Path array.
*/
public function deleteSiteAliases(PathAliasInterface $path) {
$path_alias_storage = $this->entityTypeManager->getStorage('path_alias');
$path_entities = $path_alias_storage->loadByProperties(['path' => $path->getPath()]);
$path_alias_storage->delete($path_entities);
$path_entities = $this->entityTypeManager->getStorage('path_alias')->loadByProperties(['path' => $path->getPath()]);
$this->entityTypeManager->getStorage('path_alias')->delete($path_entities);
}

}
9 changes: 4 additions & 5 deletions src/TideSiteMenuAutocreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\tide_site;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
Expand Down Expand Up @@ -228,7 +227,7 @@ protected function getAssociatedMenu($menu_title, $tid) {
*/
protected static function filterAutocreateFieldNames($values) {
return array_filter($values, function ($k) {
return Unicode::strpos($k, self::AUTOCREATE_FIELD_PREFIX) === 0 && Unicode::strpos($k, 'menu') !== FALSE;
return mb_strpos($k, self::AUTOCREATE_FIELD_PREFIX) === 0 && mb_strpos($k, 'menu') !== FALSE;
}, ARRAY_FILTER_USE_KEY);
}

Expand All @@ -252,11 +251,11 @@ protected static function makeAutocreateFieldName($string) {
* If provided string does not contain expected field prefix.
*/
protected static function extractMenuName($string) {
if (Unicode::strpos($string, self::AUTOCREATE_FIELD_PREFIX) === FALSE) {
if (mb_strpos($string, self::AUTOCREATE_FIELD_PREFIX) === FALSE) {
throw new \Exception('Unable to extract menu name from provided value');
}

return Unicode::substr($string, Unicode::strlen(self::AUTOCREATE_FIELD_PREFIX));
return mb_substr($string, mb_strlen(self::AUTOCREATE_FIELD_PREFIX));
}

/**
Expand Down Expand Up @@ -350,7 +349,7 @@ protected function makeMenuLabel($menu_title, $tid) {
*/
protected static function toMachineName($string, $delimiter = '_') {
$string = trim($string);
$string = Unicode::strtolower($string);
$string = mb_strtolower($string);
// Replace spaces and hyphens, preserving existing delimiters.
$string = str_replace([$delimiter, ' ', '-', '_'], $delimiter, $string);
// Remove all other non-alphanumeric characters.
Expand Down
122 changes: 122 additions & 0 deletions tests/src/Unit/TideSiteAliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Drupal\Tests\tide_site\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\tide_site\AliasStorageHelper;
use Drupal\tide_site\TideSiteHelper;

class TideSiteAliasTest extends UnitTestCase {

/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $entityTypeManager;

/**
* Path alias helper function.
*
* @var \Drupal\tide_site\AliasStorageHelper|\PHPUnit\Framework\MockObject\MockObject
*/
protected $aliasHelper;

/**
* Tide site helper.
*
* @var \Drupal\tide_site\TideSiteHelper|\PHPUnit\Framework\MockObject\MockObject
*/
protected $tideSiteHelper;

/**
* Entity storage.
*
* @var \PHPUnit\Framework\MockObject\MockObject|Drupal\Core\Entity\ContentEntityNullStorage
*/
protected $entityStorage;


/**
* {@inheritdoc}
*/
function setUp() {
parent::setUp();
$methods = get_class_methods('Drupal\Core\Entity\ContentEntityNullStorage');
$this->entityStorage = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityNullStorage')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
$methods = get_class_methods('Drupal\Core\Entity\EntityTypeManager');
$this->entityTypeManager = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeManager')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
$this->entityTypeManager->expects($this->any())
->method('getStorage')
->willReturn($this->entityStorage);
$this->tideSiteHelper = $this->createMock(TideSiteHelper::class);
$this->aliasHelper = new AliasStorageHelper($this->tideSiteHelper, $this->entityTypeManager);
}

function providerAliasWithoutSite() {
return [
[['alias' => '/site-1/hello'], '/hello'],
[['alias' => '/site-2/hello_1'], '/hello_1'],
[['alias' => '/site-3/hello_2'], '/hello_2'],
[['alias' => '/site-4/hello_3'], '/hello_3'],
[['alias' => '/site-5/hello_4'], '/hello_4'],
[['alias' => '/hello_5'], '/hello_5'],
[['alias' => '/node/1'], '/node/1'],
];
}

/**
* @dataProvider providerAliasWithoutSite
*/
function testgetPathAliasWithoutSitePrefixNew($alias_with_site, $expection) {
$without_siteid = $this->aliasHelper->getPathAliasWithoutSitePrefix($alias_with_site);
$this->assertEquals($expection, $without_siteid);
}

function providerIsPathHasSitePrefix() {
return [
['/site-10/hello-1', TRUE],
['/site/hello-1', FALSE],
['/10/hello-1', FALSE],
['/site-1/site-hello-1', TRUE],
['/site-10/site-2/-a1', TRUE],
];
}

/**
* @dataProvider providerIsPathHasSitePrefix
*/
function testIsPathHasSitePrefixNew($alias, $expectation) {
$methods = get_class_methods('Drupal\path_alias\Entity\PathAlias');
$path_alias = $this->getMockBuilder('Drupal\path_alias\Entity\PathAlias')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
$path_alias->expects($this->any())->method('getAlias')->willReturn($alias);
$this->assertEquals($expectation, $this->aliasHelper->isPathHasSitePrefix($path_alias));
}

function testIsAliasExists() {
$methods = get_class_methods('Drupal\path_alias\Entity\PathAlias');
$path_alias = $this->getMockBuilder('Drupal\path_alias\Entity\PathAlias')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
$path_alias->expects($this->any())
->method('getAlias')
->willReturn('/site-1/hello');
$this->entityStorage->expects($this->any())
->method('loadByProperties')
->willReturn([$path_alias]);
$this->assertEquals('/site-1/hello', $this->aliasHelper->isAliasExists('/site-1/hello')
->getAlias());
$this->assertEquals($path_alias, $this->aliasHelper->isAliasExists('/site-1/hello'));
}

}
3 changes: 1 addition & 2 deletions tests/src/Unit/TideSiteFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class TideSiteFieldsTest extends TideSiteTest {
* @dataProvider providerNormaliseFieldName
*/
public function testToMachineName($field_name, $entity_type_id, $bundle, $expected) {
$mock = self::createMock('Drupal\tide_site\TideSiteFields');
$actual = $this->callProtectedMethod($mock, 'normaliseFieldName', [$field_name, $entity_type_id, $bundle]);
$actual = TideSiteFields::normaliseFieldName($field_name, $entity_type_id, $bundle);
$this->assertEquals($expected, $actual);
}

Expand Down