diff --git a/dev-tools.sh b/dev-tools.sh index d7573df..5868a6f 100755 --- a/dev-tools.sh +++ b/dev-tools.sh @@ -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)") "$@" diff --git a/src/AliasStorageHelper.php b/src/AliasStorageHelper.php index 530a597..02a32e3 100644 --- a/src/AliasStorageHelper.php +++ b/src/AliasStorageHelper.php @@ -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); @@ -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(), @@ -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(), @@ -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(), @@ -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; } @@ -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; } @@ -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); } } diff --git a/src/TideSiteMenuAutocreate.php b/src/TideSiteMenuAutocreate.php index 5c681f4..437514f 100644 --- a/src/TideSiteMenuAutocreate.php +++ b/src/TideSiteMenuAutocreate.php @@ -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; @@ -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); } @@ -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)); } /** @@ -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. diff --git a/tests/src/Unit/TideSiteAliasTest.php b/tests/src/Unit/TideSiteAliasTest.php new file mode 100644 index 0000000..f526aae --- /dev/null +++ b/tests/src/Unit/TideSiteAliasTest.php @@ -0,0 +1,122 @@ +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')); + } + +} \ No newline at end of file diff --git a/tests/src/Unit/TideSiteFieldsTest.php b/tests/src/Unit/TideSiteFieldsTest.php index 580b7ee..7278dea 100644 --- a/tests/src/Unit/TideSiteFieldsTest.php +++ b/tests/src/Unit/TideSiteFieldsTest.php @@ -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); }