From 15bf365b96e44cf28d76affedc7c8ab1c0dafb5a Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Mon, 14 Oct 2024 10:00:27 +0700 Subject: [PATCH 1/8] 53667 speaking url for address detail pages --- plugin.php | 45 +++- plugin/AddressList.php | 38 ++- plugin/Controller/AddressDetailUrl.php | 147 +++++++++++ plugin/Gui/AdminPageApiSettings.php | 10 + plugin/Record/AddressIdRequestGuard.php | 246 ++++++++++++++++++ plugin/Utility/AddressRedirector.php | 121 +++++++++ .../AddressViewFieldModifierTypeTitle.php | 115 ++++++++ .../AddressViewFieldModifierTypes.php | 3 + 8 files changed, 713 insertions(+), 12 deletions(-) create mode 100644 plugin/Controller/AddressDetailUrl.php create mode 100644 plugin/Record/AddressIdRequestGuard.php create mode 100644 plugin/Utility/AddressRedirector.php create mode 100644 plugin/ViewFieldModifier/AddressViewFieldModifierTypeTitle.php diff --git a/plugin.php b/plugin.php index 42a591b73..9157353a3 100644 --- a/plugin.php +++ b/plugin.php @@ -66,6 +66,9 @@ use onOffice\WPlugin\Utility\Redirector; use onOffice\WPlugin\WP\WPQueryWrapper; use onOffice\WPlugin\ScriptLoader\IncludeFileModel; +use onOffice\WPlugin\Record\AddressIdRequestGuard; +use onOffice\WPlugin\Utility\AddressRedirector; +use onOffice\WPlugin\Controller\AddressDetailUrl; const DEFAULT_LIMIT_CHARACTER_TITLE = 60; @@ -282,14 +285,25 @@ function customFieldCallback( $pDI, $format, $limitEllipsis, $meta_key ) { add_filter('wpml_ls_language_url', function($url, $data) use ($pDI) { - /** @var EstateIdRequestGuard $pEstateIdGuard */ - $pEstateIdGuard = $pDI->get(EstateIdRequestGuard::class); - $pEstateDetailUrl = $pDI->get(EstateDetailUrl::class); - $oldUrl = $pDI->get(Redirector::class)->getCurrentLink(); $pWPQueryWrapper = $pDI->get(WPQueryWrapper::class); $estateId = (int) $pWPQueryWrapper->getWPQuery()->get('estate_id', 0); - - return $pEstateIdGuard->createEstateDetailLinkForSwitchLanguageWPML($url, $estateId, $pEstateDetailUrl, $oldUrl, $data['default_locale']); + $addressId = (int) $pWPQueryWrapper->getWPQuery()->get('address_id', 0); + + if (!empty($estateId)) { + /** @var EstateIdRequestGuard $pEstateIdGuard */ + $pEstateIdGuard = $pDI->get(EstateIdRequestGuard::class); + $pEstateDetailUrl = $pDI->get(EstateDetailUrl::class); + $oldUrl = $pDI->get(Redirector::class)->getCurrentLink(); + return $pEstateIdGuard->createEstateDetailLinkForSwitchLanguageWPML($url, $estateId, $pEstateDetailUrl, $oldUrl, $data['default_locale']); + } + + if (!empty($addressId)) { + /** @var AddressIdRequestGuard $pAddressIdGuard */ + $pAddressIdGuard = $pDI->get(AddressIdRequestGuard::class); + $pEstateDetailUrl = $pDI->get(AddressDetailUrl::class); + $oldUrl = $pDI->get(AddressRedirector::class)->getCurrentLink(); + return $pAddressIdGuard->createAddressDetailLinkForSwitchLanguageWPML($url, $addressId, $pEstateDetailUrl, $oldUrl, $data['default_locale']); + } }, 10, 2); register_activation_hook(__FILE__, [Installer::class, 'install']); @@ -397,6 +411,25 @@ function custom_cron_schedules($schedules) { } }); +$pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', true); + +add_action('parse_request', function(WP $pWP) use ($pDI, $pAddressRedirection) { + $addressId = $pWP->query_vars['address_id'] ?? ''; + /** @var AddressIdRequestGuard $pEstateIdGuard */ + $pAddressIdGuard = $pDI->get(AddressIdRequestGuard::class); + + if ($addressId !== '') { + $addressId = (int)$addressId; + + if ($addressId === 0 || !$pAddressIdGuard->isValid($addressId)) { + $pWP->handle_404(); + include(get_query_template('404')); + die(); + } + $pAddressIdGuard->addressDetailUrlChecker( $addressId, $pDI->get( AddressRedirector::class ), $pAddressRedirection); + } +}); + add_action('parse_request', static function(WP $pWP) use ($pDI) { if (isset($pWP->query_vars['onoffice_estate_type_json'])) { $content = wp_json_encode($pDI->get(EstateKindTypeReader::class)->read()); diff --git a/plugin/AddressList.php b/plugin/AddressList.php index f66f098d6..b2e243a36 100644 --- a/plugin/AddressList.php +++ b/plugin/AddressList.php @@ -45,6 +45,7 @@ use function esc_html; use onOffice\WPlugin\Field\UnknownFieldException; use onOffice\WPlugin\DataView\DataAddressDetailView; +use onOffice\WPlugin\Controller\AddressDetailUrl; /** * @@ -145,6 +146,9 @@ class AddressList /** @var FieldsCollection */ private $_pFieldsCollection = []; + /** @var AddressDetailUrl */ + private $_pLanguageSwitcher; + /** * * @param DataViewAddress $pDataViewAddress @@ -159,6 +163,7 @@ public function __construct(DataViewAddress $pDataViewAddress = null, AddressLis $pSDKWrapper = $this->_pEnvironment->getSDKWrapper(); $this->_pApiClientAction = new APIClientActionGeneric ($pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'address'); + $this->_pLanguageSwitcher = new AddressDetailUrl(); } /** @@ -560,15 +565,36 @@ public function generateImageAlt(int $addressId): string public function getAddressLink(string $addressId): string { - $pageId = $this->_pEnvironment->getDataAddressDetailViewHandler() - ->getAddressDetailView()->getPageId(); + $pageId = $this->_pEnvironment->getDataAddressDetailViewHandler() + ->getAddressDetailView()->getPageId(); + + if ( $pageId !== 0 ) { + $currentAddress = $this->getAddressById($addressId); + $firstName = $currentAddress['Vorname']; + $lastName = $currentAddress['Name']; + $company = $currentAddress['Zusatz1']; + $parts = []; + if (!empty($firstName)) { + $parts[] = strtolower($firstName); + } + if (!empty($lastName)) { + $parts[] = strtolower($lastName); + } + if (!empty($company)) { + $parts[] = strtolower($company); + } + $addressTitle = implode('-', $parts); - $url = get_page_link( $pageId ) . $addressId; - $fullLinkElements = parse_url( $url ); + $url = get_page_link( $pageId ); + $fullLink = $this->_pLanguageSwitcher->createAddressDetailLink( $url, $addressId, $addressTitle ); + + $fullLinkElements = parse_url( $fullLink ); if ( empty( $fullLinkElements['query'] ) ) { - $url .= '/'; + $fullLink .= '/'; } - return $url; + } + + return $fullLink; } /** diff --git a/plugin/Controller/AddressDetailUrl.php b/plugin/Controller/AddressDetailUrl.php new file mode 100644 index 000000000..9ca3e189b --- /dev/null +++ b/plugin/Controller/AddressDetailUrl.php @@ -0,0 +1,147 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\WPlugin\Controller; + + +class AddressDetailUrl +{ + /** + * @param string $url + * @param int $addressId + * @param string|null $title + * @param string|null $oldUrl + * @param bool $flag + * + * @return string + */ + + public function createAddressDetailLink( + string $url, + int $addressId, + string $title = null, + string $oldUrl = null, + bool $flag = false): string + { + $urlLsSwitcher = $url; + $slashChar = ''; + + if ( $addressId !== 0 ) { + $urlElements = parse_url( $url ); + $getParameters = []; + + if ( ! empty( $urlElements['query'] ) ) { + parse_str( $urlElements['query'], $getParameters ); + } + + if ( ! is_null( $oldUrl ) ) { + $oldUrlElements = parse_url( $oldUrl ); + $oldUrlPathArr = explode( '/', $oldUrlElements['path'] ); + if ( empty( end( $oldUrlPathArr ) ) || $flag ) { + $slashChar = '/'; + } + } + + $urlTemp = $addressId; + + if ( ! empty( $title ) && $this->isOptionShowTitleUrl() ) { + $urlTemp .= $this->getSanitizeTitle( $title, $flag ); + } + + $urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $urlElements['path'] . $urlTemp . $slashChar; + + if ( ! empty( $getParameters ) ) { + $urlLsSwitcher .= '?' . http_build_query( $getParameters ); + } + } + + return $urlLsSwitcher; + } + + + /** + * + * @return bool + * + */ + + public function isOptionShowTitleUrl() + { + return get_option('onoffice-address-detail-view-showInfoUserUrl', false); + } + + + /** + * @param string $title + * @param bool $flag + * @return string + */ + + public function getSanitizeTitle(string $title, bool $flag = false): string + { + $sanitizeTitle = $flag ? sanitize_title(remove_accents($title)) : sanitize_title($title); + return '-' . $sanitizeTitle; + } + + /** + * @param int $addressId + * @param string|null $title + * @param string|null $oldUrl + * @param bool $isUrlHaveTitle + * @param bool $pAddressRedirection + * @return string + */ + public function getUrlWithAddressTitle(int $addressId, string $title = null, string $oldUrl = null, bool $isUrlHaveTitle = false, bool $pAddressRedirection = false): string + { + $getParameters = []; + $urlElements = parse_url( $oldUrl ); + $urlTemp = $addressId; + + if (!empty($title) && $this->isOptionShowTitleUrl()) { + if ($pAddressRedirection === false && !empty($urlElements['query']) && !$isUrlHaveTitle) { + $urlTemp .= ''; + } else { + $urlTemp .= $this->getSanitizeTitle($title); + } + } + + if ( ! empty( $urlElements['query'] ) ) { + parse_str( $urlElements['query'], $getParameters ); + } + + $oldUrlPathArr = explode( '/', $urlElements['path'] ); + if ( empty( end( $oldUrlPathArr ) ) ) { + array_pop( $oldUrlPathArr ); + } + array_pop( $oldUrlPathArr ); + $newPath = implode( '/', $oldUrlPathArr ); + + $urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $newPath . '/' . $urlTemp; + + if (!empty($getParameters)) { + $urlLsSwitcher = add_query_arg($getParameters,$urlLsSwitcher); + } + + return $urlLsSwitcher; + } +} diff --git a/plugin/Gui/AdminPageApiSettings.php b/plugin/Gui/AdminPageApiSettings.php index e1ab2cffa..fcef222de 100644 --- a/plugin/Gui/AdminPageApiSettings.php +++ b/plugin/Gui/AdminPageApiSettings.php @@ -491,8 +491,18 @@ private function addFormModelDetailView(string $pageSlug) $pInputModelShowTitleUrl->setValue(get_option($pInputModelShowTitleUrl->getIdentifier()) == 1); $pInputModelShowTitleUrl->setDescriptionTextHTML(__('If this checkbox is selected, the title of the property will be part of the URLs of the detail views. The title is placed after the record number, e.g. /1234-nice-location-with-view. No more than the first five words of the title are used.', 'onoffice-for-wp-websites')); + $groupSlugView = 'onoffice-address-detail-view'; + $showTitleInUrl = __('Enable "Speaking" URL in Address Detail', 'onoffice-for-wp-websites'); + $pInputModelShowInfoContactUrl = new InputModelOption($groupSlugView, 'showInfoUserUrl', + $showTitleInUrl, InputModelOption::SETTING_TYPE_BOOLEAN); + $pInputModelShowInfoContactUrl->setHtmlType(InputModelOption::HTML_TYPE_CHECKBOX); + $pInputModelShowInfoContactUrl->setValuesAvailable(1); + $pInputModelShowInfoContactUrl->setValue(get_option($pInputModelShowInfoContactUrl->getIdentifier()) == 1); + $pInputModelShowInfoContactUrl->setDescriptionTextHTML(__('If this checkbox is selected, the name and company of the address will be included in the URLs of the address detail views. The name and company will be placed after the record number, eg. /1234-firstname-lastname-company.', 'onoffice-for-wp-websites')); + $pFormModel = new FormModel(); $pFormModel->addInputModel($pInputModelShowTitleUrl); + $pFormModel->addInputModel($pInputModelShowInfoContactUrl); $pFormModel->setGroupSlug($groupSlugView); $pFormModel->setPageSlug($pageSlug); $pFormModel->setLabel(__('Detail View URLs', 'onoffice-for-wp-websites')); diff --git a/plugin/Record/AddressIdRequestGuard.php b/plugin/Record/AddressIdRequestGuard.php new file mode 100644 index 000000000..9191d01b6 --- /dev/null +++ b/plugin/Record/AddressIdRequestGuard.php @@ -0,0 +1,246 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\WPlugin\Record; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\SDK\onOfficeSDK; +use onOffice\WPlugin\API\APIClientActionGeneric; +use onOffice\WPlugin\Controller\AddressDetailUrl; +use onOffice\WPlugin\Factory\AddressListFactory; +use onOffice\WPlugin\Language; +use onOffice\WPlugin\SDKWrapper; +use onOffice\WPlugin\Utility\AddressRedirector; + +/** + * + * Checks if an address ID exists + * + */ + +class AddressIdRequestGuard +{ + /** @var AddressListFactory */ + private $_pAddressDetailFactory; + + /** * @var ArrayContainerEscape */ + private $_addressData; + + /** @var array */ + private $_addressDataWPML = []; + + /** @var Container */ + private $_pContainer = null; + + /** @var array */ + private $_activeLanguages = []; + + /** + * + * @param AddressListFactory $pAddressDetailFactory + * @param Container|null $pContainer + * + */ + + public function __construct(AddressListFactory $pAddressDetailFactory, Container $pContainer = null) + { + $this->_pAddressDetailFactory = $pAddressDetailFactory; + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + $this->_pContainer = $pContainer ?? $pContainerBuilder->build(); + } + + + /** + * @param int $addressId + * @return bool + */ + + public function isValid(int $addressId): bool + { + $pAddressDetail = $this->_pAddressDetailFactory->createAddressDetail($addressId); + $pAddressDetail->loadSingleAddress($addressId); + + $this->_addressData = $pAddressDetail->getRows(); + + if ($this->isActiveWPML()) { + $this->_activeLanguages = apply_filters('wpml_active_languages', null); + if ($addressId > 0 && !empty($this->_addressData) && count($this->_activeLanguages) > 1) { + $this->addressDataForWPML($addressId); + } + } + + return $this->_addressData !== false; + } + + + /** + * + * @param int $addressId + * @param AddressRedirector $pRedirector + * @param bool $pAddressRedirection + * + * @return void + */ + + public function addressDetailUrlChecker( int $addressId, AddressRedirector $pRedirector, bool $pAddressRedirection ) + { + $firstName = $this->_addressData[$addressId]->getValue('Vorname'); + $lastName = $this->_addressData[$addressId]->getValue('Name'); + $company = $this->_addressData[$addressId]->getValue('Zusatz1'); + $addressTitle = $this->createAddressTitle($firstName, $lastName, $company); + $pRedirector->redirectDetailView($addressId, $addressTitle, $pAddressRedirection); + } + + /** + * @param string $url + * @param int $addressId + * @param AddressDetailUrl $pAddressDetailUrl + * @param string $oldUrl + * @param string $switchLocale + * + * @return string + */ + public function createAddressDetailLinkForSwitchLanguageWPML(string $url, int $addressId, AddressDetailUrl $pAddressDetailUrl, string $oldUrl, string $switchLocale): string + { + $addressDetailTitle = ''; + $currentLocale = get_locale(); + if ($addressId > 0 && !empty($this->_addressData)) { + if ($switchLocale !== $currentLocale) { + $this->addSwitchFilterToLocaleHook($switchLocale); + $addressDetailTitle = $this->_addressDataWPML[$switchLocale]; + } else { + $firstName = $this->_addressData[$addressId]->getValue('Vorname'); + $lastName = $this->_addressData[$addressId]->getValue('Name'); + $company = $this->_addressData[$addressId]->getValue('Zusatz1'); + $addressDetailTitle = $this->createAddressTitle($firstName, $lastName, $company); + } + } + $detailLinkForWPML = $pAddressDetailUrl->createAddressDetailLink($url, $addressId, $addressDetailTitle, $oldUrl, true); + $this->addSwitchFilterToLocaleHook($currentLocale); + + return $detailLinkForWPML; + } + + /** + * @param array $locales + * @param int $addressId + * @return array + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + private function getAddressTitleByLocales(array $locales, int $addressId): array + { + $pApiClientActionClone = null; + $addressTitleByLocales = []; + $listRequestInQueue = []; + + $pSDKWrapper = $this->_pContainer->get(SDKWrapper::class); + $pApiClientAction = new APIClientActionGeneric + ($pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'address'); + foreach ($locales as $locale) { + $isoLanguageCode = Language::LOCALE_MAPPING[$locale] ?? 'DEU'; + $addressParameters = [ + 'data' => ['Vorname', 'Name' , 'Zusatz1'], + 'addresslanguage' => $isoLanguageCode, + 'outputlanguage' => $isoLanguageCode, + ]; + $pApiClientActionClone = clone $pApiClientAction; + $pApiClientActionClone->setResourceId((string)$addressId); + $pApiClientActionClone->setParameters($addressParameters); + $pApiClientActionClone->addRequestToQueue(); + $listRequestInQueue[$locale] = $pApiClientActionClone; + } + $pApiClientActionClone->sendRequests(); + + if (empty($pApiClientActionClone->getResultRecords())) { + return []; + } + + foreach($listRequestInQueue as $key => $pApiClientAction) { + $firstName = $pApiClientAction->getResultRecords()[0]['elements']['Vorname']; + $lastName = $pApiClientAction->getResultRecords()[0]['elements']['Name']; + $company = $pApiClientAction->getResultRecords()[0]['elements']['Zusatz1']; + $addressTitleByLocales[$key] = $this->createAddressTitle($firstName, $lastName, $company); + } + + return $addressTitleByLocales; + } + + /** + * @param int $addressId + * @return void + */ + private function addressDataForWPML(int $addressId) + { + $defaultLocales = []; + foreach ($this->_activeLanguages as $language) { + if (isset($language['default_locale']) && $language['default_locale'] !== get_locale()) { + $defaultLocales[] = $language['default_locale']; + } + } + + $this->_addressDataWPML = $this->getAddressTitleByLocales($defaultLocales, $addressId); + } + + /** + * @return bool + */ + private function isActiveWPML(): bool + { + return in_array('sitepress-multilingual-cms/sitepress.php', get_option('active_plugins')); + } + + /** + * @param string $locale + */ + private function addSwitchFilterToLocaleHook(string $locale) + { + add_filter('locale', function () use ($locale) { + return $locale; + }); + } + + /** + * @param string|null $firstName + * @param string|null $lastName + * @param string|null $company + * @return string + */ + private function createAddressTitle(string $firstName, string $lastName, string $company): string + { + $parts = []; + if (!empty($firstName)) { + $parts[] = strtolower($firstName); + } + if (!empty($lastName)) { + $parts[] = strtolower($lastName); + } + if (!empty($company)) { + $parts[] = strtolower($company); + } + + return implode('-', $parts); + } +} diff --git a/plugin/Utility/AddressRedirector.php b/plugin/Utility/AddressRedirector.php new file mode 100644 index 000000000..e4acd2ebc --- /dev/null +++ b/plugin/Utility/AddressRedirector.php @@ -0,0 +1,121 @@ +_wpAddressDetailUrl = $addressDetailUrl; + $this->_wpRedirectWrapper = $redirectWrapper; + } + + + /** + * @param $addressId + * @param $addressTitle + * @param $pAddressRedirection + * @return bool|void + */ + + public function redirectDetailView( $addressId, $addressTitle, $pAddressRedirection ) + { + $matches = $this->checkUrlIsMatchRule(); + if ( empty( $matches[2] ) ) { + return true; + } + + $oldUrl = $this->getCurrentLink(); + $sanitizeTitle = $this->_wpAddressDetailUrl->getSanitizeTitle($addressTitle); + $isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; + $newUrl = $this->_wpAddressDetailUrl->getUrlWithAddressTitle($addressId, $addressTitle, $oldUrl, $isUrlHaveTitle, $pAddressRedirection); + if ( $newUrl !== $oldUrl ) { + $isNewUrlValid = $this->checkNewUrlIsValid( + array_filter( explode( '/', $newUrl ) ), + array_filter( explode( '/', $oldUrl ) ) + ); + + if ( $isNewUrlValid ) { + $this->_wpRedirectWrapper->redirect( $newUrl ); + } + } + } + + + /** + * @return mixed + */ + + public function checkUrlIsMatchRule() + { + $uri = $this->getUri(); + $uriToArray = explode( '/', $uri ); + array_pop( $uriToArray ); + $pagePath = implode( '/', array_filter( $uriToArray ) ); + + //Check pass rule and has Unique ID + preg_match( '/^(' . preg_quote( $pagePath, + '/' ) . ')\/([0-9]+)(-([^$]+)?)?\/?$/', $uri, $matches ); + + return $matches; + } + + + /** + * @param array $newUrlArr + * @param array $oldUrlArr + * + * @return bool + */ + + public function checkNewUrlIsValid( array $newUrlArr, array $oldUrlArr ) + { + if ( end( $newUrlArr ) !== end( $oldUrlArr ) ) { + array_pop( $newUrlArr ); + array_pop( $oldUrlArr ); + + return empty( array_diff( $newUrlArr, $oldUrlArr ) ); + } + + return false; + } + + + /** + * @return mixed + */ + + public function getUri() + { + global $wp; + + return $wp->request; + } + + + /** + * @return string + */ + + public function getCurrentLink(): string + { + global $wp; + return home_url(add_query_arg($_GET, $wp->request)); + } +} diff --git a/plugin/ViewFieldModifier/AddressViewFieldModifierTypeTitle.php b/plugin/ViewFieldModifier/AddressViewFieldModifierTypeTitle.php new file mode 100644 index 000000000..81b4d7491 --- /dev/null +++ b/plugin/ViewFieldModifier/AddressViewFieldModifierTypeTitle.php @@ -0,0 +1,115 @@ +. + * + */ + +namespace onOffice\WPlugin\ViewFieldModifier; +use onOffice\WPlugin\DataView\DataAddressDetailViewHandler; +/** + * + * @url http://www.onoffice.de + * @copyright 2003-2024, onOffice(R) GmbH + * + */ + +class AddressViewFieldModifierTypeTitle + implements ViewFieldModifierTypeBase +{ + /** @var array */ + private $_viewFields = []; + + /** + * + * @param array $viewFields + * + */ + + public function __construct(array $viewFields) + { + $this->_viewFields = $viewFields; + } + + + /** + * + * @return array + * + */ + + public function getAPIFields(): array + { + $titleFields = [ + 'Vorname', + 'Name', + 'Zusatz1' + ]; + + return array_values(array_unique(array_merge($this->_viewFields, $titleFields))); + } + + /** + * + * @return array + * + */ + + public function getAPICustomFields(): array + { + $pDataAddressDetailViewHandler = new DataAddressDetailViewHandler(); + $dataCustomFields = $pDataAddressDetailViewHandler->getAddressDetailView(); + + return $dataCustomFields->getFields(); + } + + + /** + * + * @return array + * + */ + + public function getVisibleFields(): array + { + return $this->getAPIFields(); + } + + /** + * + * @return array + * + */ + + public function getVisibleCustomFields(): array + { + return $this->getAPICustomFields(); + } + + /** + * + * @param array $record + * @return array + * + */ + + public function reduceRecord(array $record): array + { + // nothing to do + return $record; + } +} diff --git a/plugin/ViewFieldModifier/AddressViewFieldModifierTypes.php b/plugin/ViewFieldModifier/AddressViewFieldModifierTypes.php index ec4af9710..799299cc0 100644 --- a/plugin/ViewFieldModifier/AddressViewFieldModifierTypes.php +++ b/plugin/ViewFieldModifier/AddressViewFieldModifierTypes.php @@ -34,10 +34,13 @@ class AddressViewFieldModifierTypes /** */ const MODIFIER_TYPE_DEFAULT = 'modifierTypeDefault'; + /** */ + const MODIFIER_TYPE_TITLE = 'modifierTypeTitle'; /** @var array */ private $_mapping = [ self::MODIFIER_TYPE_DEFAULT => AddressViewFieldModifierTypeDefault::class, + self::MODIFIER_TYPE_TITLE => AddressViewFieldModifierTypeTitle::class, ]; From 20573b26cab168d5c5a13e7361e82f0f795466f8 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Mon, 14 Oct 2024 14:00:10 +0700 Subject: [PATCH 2/8] 53667 refactor code & update unit test --- plugin/AddressList.php | 46 ++-- plugin/Controller/AddressDetailUrl.php | 52 ++-- plugin/Installer/Installer.php | 1 + plugin/Record/AddressIdRequestGuard.php | 91 ++++--- plugin/Utility/AddressRedirector.php | 53 ++-- tests/TestClassAddressDetailUrl.php | 209 +++++++++++++++ tests/TestClassAddressIdRequestGuard.php | 253 ++++++++++++++++++ tests/TestClassAddressList.php | 76 ++++-- tests/TestClassAddressRedirectIfOldUrl.php | 116 ++++++++ ...ClassAddressViewFieldModifierTypeTitle.php | 143 ++++++++++ 10 files changed, 901 insertions(+), 139 deletions(-) create mode 100644 tests/TestClassAddressDetailUrl.php create mode 100644 tests/TestClassAddressIdRequestGuard.php create mode 100644 tests/TestClassAddressRedirectIfOldUrl.php create mode 100644 tests/TestClassAddressViewFieldModifierTypeTitle.php diff --git a/plugin/AddressList.php b/plugin/AddressList.php index b2e243a36..9bfc6cb8d 100644 --- a/plugin/AddressList.php +++ b/plugin/AddressList.php @@ -563,35 +563,37 @@ public function generateImageAlt(int $addressId): string return $imageAlt; } + /** + * @param string $addressId + * @return string + */ public function getAddressLink(string $addressId): string { $pageId = $this->_pEnvironment->getDataAddressDetailViewHandler() ->getAddressDetailView()->getPageId(); - if ( $pageId !== 0 ) { - $currentAddress = $this->getAddressById($addressId); - $firstName = $currentAddress['Vorname']; - $lastName = $currentAddress['Name']; - $company = $currentAddress['Zusatz1']; - $parts = []; - if (!empty($firstName)) { - $parts[] = strtolower($firstName); - } - if (!empty($lastName)) { - $parts[] = strtolower($lastName); - } - if (!empty($company)) { - $parts[] = strtolower($company); - } - $addressTitle = implode('-', $parts); + $currentAddress = $this->getAddressById($addressId); + $firstName = $currentAddress['Vorname'] ?? ''; + $lastName = $currentAddress['Name'] ?? ''; + $company = $currentAddress['Zusatz1'] ?? ''; + $parts = []; + if (!empty($firstName)) { + $parts[] = strtolower($firstName); + } + if (!empty($lastName)) { + $parts[] = strtolower($lastName); + } + if (!empty($company)) { + $parts[] = strtolower($company); + } + $addressTitle = implode(' ', $parts); - $url = get_page_link( $pageId ); - $fullLink = $this->_pLanguageSwitcher->createAddressDetailLink( $url, $addressId, $addressTitle ); + $url = get_page_link( $pageId ); + $fullLink = $this->_pLanguageSwitcher->createAddressDetailLink( $url, $addressId, $addressTitle ); - $fullLinkElements = parse_url( $fullLink ); - if ( empty( $fullLinkElements['query'] ) ) { - $fullLink .= '/'; - } + $fullLinkElements = parse_url( $fullLink ); + if ( empty( $fullLinkElements['query'] ) ) { + $fullLink .= '/'; } return $fullLink; diff --git a/plugin/Controller/AddressDetailUrl.php b/plugin/Controller/AddressDetailUrl.php index 9ca3e189b..a5ce7c1c0 100644 --- a/plugin/Controller/AddressDetailUrl.php +++ b/plugin/Controller/AddressDetailUrl.php @@ -38,40 +38,40 @@ class AddressDetailUrl public function createAddressDetailLink( string $url, - int $addressId, + int $addressId, string $title = null, string $oldUrl = null, - bool $flag = false): string + bool $flag = false): string { $urlLsSwitcher = $url; - $slashChar = ''; + $slashChar = ''; - if ( $addressId !== 0 ) { - $urlElements = parse_url( $url ); + if ($addressId !== 0) { + $urlElements = parse_url($url); $getParameters = []; - if ( ! empty( $urlElements['query'] ) ) { - parse_str( $urlElements['query'], $getParameters ); + if (!empty($urlElements['query'])) { + parse_str($urlElements['query'], $getParameters); } - if ( ! is_null( $oldUrl ) ) { - $oldUrlElements = parse_url( $oldUrl ); - $oldUrlPathArr = explode( '/', $oldUrlElements['path'] ); - if ( empty( end( $oldUrlPathArr ) ) || $flag ) { + if (!is_null($oldUrl)) { + $oldUrlElements = parse_url($oldUrl); + $oldUrlPathArr = explode('/', $oldUrlElements['path']); + if (empty(end($oldUrlPathArr)) || $flag) { $slashChar = '/'; } } $urlTemp = $addressId; - if ( ! empty( $title ) && $this->isOptionShowTitleUrl() ) { - $urlTemp .= $this->getSanitizeTitle( $title, $flag ); + if (!empty($title) && $this->isOptionShowTitleUrl()) { + $urlTemp .= $this->getSanitizeTitle($title, $flag); } $urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $urlElements['path'] . $urlTemp . $slashChar; - if ( ! empty( $getParameters ) ) { - $urlLsSwitcher .= '?' . http_build_query( $getParameters ); + if (!empty($getParameters)) { + $urlLsSwitcher .= '?' . http_build_query($getParameters); } } @@ -87,7 +87,7 @@ public function createAddressDetailLink( public function isOptionShowTitleUrl() { - return get_option('onoffice-address-detail-view-showInfoUserUrl', false); + return get_option('onoffice-address-detail-view-showInfoUserUrl', false); } @@ -114,8 +114,8 @@ public function getSanitizeTitle(string $title, bool $flag = false): string public function getUrlWithAddressTitle(int $addressId, string $title = null, string $oldUrl = null, bool $isUrlHaveTitle = false, bool $pAddressRedirection = false): string { $getParameters = []; - $urlElements = parse_url( $oldUrl ); - $urlTemp = $addressId; + $urlElements = parse_url($oldUrl); + $urlTemp = $addressId; if (!empty($title) && $this->isOptionShowTitleUrl()) { if ($pAddressRedirection === false && !empty($urlElements['query']) && !$isUrlHaveTitle) { @@ -125,21 +125,21 @@ public function getUrlWithAddressTitle(int $addressId, string $title = null, str } } - if ( ! empty( $urlElements['query'] ) ) { - parse_str( $urlElements['query'], $getParameters ); + if (!empty($urlElements['query'])) { + parse_str($urlElements['query'], $getParameters); } - $oldUrlPathArr = explode( '/', $urlElements['path'] ); - if ( empty( end( $oldUrlPathArr ) ) ) { - array_pop( $oldUrlPathArr ); + $oldUrlPathArr = explode('/', $urlElements['path']); + if (empty(end($oldUrlPathArr))) { + array_pop($oldUrlPathArr); } - array_pop( $oldUrlPathArr ); - $newPath = implode( '/', $oldUrlPathArr ); + array_pop($oldUrlPathArr); + $newPath = implode('/', $oldUrlPathArr); $urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $newPath . '/' . $urlTemp; if (!empty($getParameters)) { - $urlLsSwitcher = add_query_arg($getParameters,$urlLsSwitcher); + $urlLsSwitcher = add_query_arg($getParameters, $urlLsSwitcher); } return $urlLsSwitcher; diff --git a/plugin/Installer/Installer.php b/plugin/Installer/Installer.php index 870f0ed72..bb5cad881 100644 --- a/plugin/Installer/Installer.php +++ b/plugin/Installer/Installer.php @@ -119,6 +119,7 @@ static public function deinstall() delete_option('onoffice-settings-opengraph'); delete_option('onoffice-settings-twittercards'); delete_option('onoffice-settings-duration-cache'); + delete_option('onoffice-address-detail-view-showInfoUserUrl'); self::flushRules(); } diff --git a/plugin/Record/AddressIdRequestGuard.php b/plugin/Record/AddressIdRequestGuard.php index 9191d01b6..6e9fc2078 100644 --- a/plugin/Record/AddressIdRequestGuard.php +++ b/plugin/Record/AddressIdRequestGuard.php @@ -53,7 +53,7 @@ class AddressIdRequestGuard /** @var Container */ private $_pContainer = null; - /** @var array */ + /** @var array */ private $_activeLanguages = []; /** @@ -82,7 +82,7 @@ public function isValid(int $addressId): bool $pAddressDetail = $this->_pAddressDetailFactory->createAddressDetail($addressId); $pAddressDetail->loadSingleAddress($addressId); - $this->_addressData = $pAddressDetail->getRows(); + $this->_addressData = $pAddressDetail->getRows()[ $addressId ]; if ($this->isActiveWPML()) { $this->_activeLanguages = apply_filters('wpml_active_languages', null); @@ -97,19 +97,19 @@ public function isValid(int $addressId): bool /** * - * @param int $addressId - * @param AddressRedirector $pRedirector + * @param int $addressId + * @param AddressRedirector $pRedirector * @param bool $pAddressRedirection * * @return void */ - public function addressDetailUrlChecker( int $addressId, AddressRedirector $pRedirector, bool $pAddressRedirection ) + public function addressDetailUrlChecker(int $addressId, AddressRedirector $pRedirector, bool $pAddressRedirection) { - $firstName = $this->_addressData[$addressId]->getValue('Vorname'); - $lastName = $this->_addressData[$addressId]->getValue('Name'); - $company = $this->_addressData[$addressId]->getValue('Zusatz1'); - $addressTitle = $this->createAddressTitle($firstName, $lastName, $company); + $firstName = $this->_addressData->getValue('Vorname'); + $lastName = $this->_addressData->getValue('Name'); + $company = $this->_addressData->getValue('Zusatz1'); + $addressTitle = $this->createAddressTitle($firstName, $lastName, $company); $pRedirector->redirectDetailView($addressId, $addressTitle, $pAddressRedirection); } @@ -129,12 +129,12 @@ public function createAddressDetailLinkForSwitchLanguageWPML(string $url, int $a if ($addressId > 0 && !empty($this->_addressData)) { if ($switchLocale !== $currentLocale) { $this->addSwitchFilterToLocaleHook($switchLocale); - $addressDetailTitle = $this->_addressDataWPML[$switchLocale]; + $addressDetailTitle = $this->_addressDataWPML[ $switchLocale ]; } else { - $firstName = $this->_addressData[$addressId]->getValue('Vorname'); - $lastName = $this->_addressData[$addressId]->getValue('Name'); - $company = $this->_addressData[$addressId]->getValue('Zusatz1'); - $addressDetailTitle = $this->createAddressTitle($firstName, $lastName, $company); + $firstName = $this->_addressData->getValue('Vorname'); + $lastName = $this->_addressData->getValue('Name'); + $company = $this->_addressData->getValue('Zusatz1'); + $addressDetailTitle = $this->createAddressTitle($firstName, $lastName, $company); } } $detailLinkForWPML = $pAddressDetailUrl->createAddressDetailLink($url, $addressId, $addressDetailTitle, $oldUrl, true); @@ -160,17 +160,16 @@ private function getAddressTitleByLocales(array $locales, int $addressId): array $pApiClientAction = new APIClientActionGeneric ($pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'address'); foreach ($locales as $locale) { - $isoLanguageCode = Language::LOCALE_MAPPING[$locale] ?? 'DEU'; + $isoLanguageCode = Language::LOCALE_MAPPING[ $locale ] ?? 'DEU'; $addressParameters = [ - 'data' => ['Vorname', 'Name' , 'Zusatz1'], - 'addresslanguage' => $isoLanguageCode, - 'outputlanguage' => $isoLanguageCode, + 'data' => ['Vorname', 'Name', 'Zusatz1'], + 'outputlanguage' => $isoLanguageCode ]; $pApiClientActionClone = clone $pApiClientAction; - $pApiClientActionClone->setResourceId((string)$addressId); + $pApiClientActionClone->setResourceId((string) $addressId); $pApiClientActionClone->setParameters($addressParameters); $pApiClientActionClone->addRequestToQueue(); - $listRequestInQueue[$locale] = $pApiClientActionClone; + $listRequestInQueue[ $locale ] = $pApiClientActionClone; } $pApiClientActionClone->sendRequests(); @@ -178,11 +177,11 @@ private function getAddressTitleByLocales(array $locales, int $addressId): array return []; } - foreach($listRequestInQueue as $key => $pApiClientAction) { - $firstName = $pApiClientAction->getResultRecords()[0]['elements']['Vorname']; - $lastName = $pApiClientAction->getResultRecords()[0]['elements']['Name']; - $company = $pApiClientAction->getResultRecords()[0]['elements']['Zusatz1']; - $addressTitleByLocales[$key] = $this->createAddressTitle($firstName, $lastName, $company); + foreach ($listRequestInQueue as $key => $pApiClientAction) { + $firstName = $pApiClientAction->getResultRecords()[0]['elements']['Vorname']; + $lastName = $pApiClientAction->getResultRecords()[0]['elements']['Name']; + $company = $pApiClientAction->getResultRecords()[0]['elements']['Zusatz1']; + $addressTitleByLocales[ $key ] = $this->createAddressTitle($firstName, $lastName, $company); } return $addressTitleByLocales; @@ -222,25 +221,25 @@ private function addSwitchFilterToLocaleHook(string $locale) }); } - /** - * @param string|null $firstName - * @param string|null $lastName - * @param string|null $company - * @return string - */ - private function createAddressTitle(string $firstName, string $lastName, string $company): string - { - $parts = []; - if (!empty($firstName)) { - $parts[] = strtolower($firstName); - } - if (!empty($lastName)) { - $parts[] = strtolower($lastName); - } - if (!empty($company)) { - $parts[] = strtolower($company); - } - - return implode('-', $parts); - } + /** + * @param string|null $firstName + * @param string|null $lastName + * @param string|null $company + * @return string + */ + private function createAddressTitle(string $firstName, string $lastName, string $company): string + { + $parts = []; + if (!empty($firstName)) { + $parts[] = strtolower($firstName); + } + if (!empty($lastName)) { + $parts[] = strtolower($lastName); + } + if (!empty($company)) { + $parts[] = strtolower($company); + } + + return implode(' ', $parts); + } } diff --git a/plugin/Utility/AddressRedirector.php b/plugin/Utility/AddressRedirector.php index e4acd2ebc..a03af376a 100644 --- a/plugin/Utility/AddressRedirector.php +++ b/plugin/Utility/AddressRedirector.php @@ -3,7 +3,6 @@ namespace onOffice\WPlugin\Utility; use onOffice\WPlugin\Controller\AddressDetailUrl; -use onOffice\WPlugin\WP\WPPageWrapper; use onOffice\WPlugin\WP\WPRedirectWrapper; class AddressRedirector @@ -16,11 +15,11 @@ class AddressRedirector /** - * @param AddressDetailUrl $addressDetailUrl - * @param WPRedirectWrapper $redirectWrapper + * @param AddressDetailUrl $addressDetailUrl + * @param WPRedirectWrapper $redirectWrapper */ - public function __construct( AddressDetailUrl $addressDetailUrl, WPRedirectWrapper $redirectWrapper ) + public function __construct(AddressDetailUrl $addressDetailUrl, WPRedirectWrapper $redirectWrapper) { $this->_wpAddressDetailUrl = $addressDetailUrl; $this->_wpRedirectWrapper = $redirectWrapper; @@ -28,16 +27,16 @@ public function __construct( AddressDetailUrl $addressDetailUrl, WPRedirectWrapp /** - * @param $addressId - * @param $addressTitle - * @param $pAddressRedirection + * @param int $addressId + * @param string $addressTitle + * @param bool $pAddressRedirection * @return bool|void */ - public function redirectDetailView( $addressId, $addressTitle, $pAddressRedirection ) + public function redirectDetailView(int $addressId, string $addressTitle, bool $pAddressRedirection) { $matches = $this->checkUrlIsMatchRule(); - if ( empty( $matches[2] ) ) { + if (empty($matches[2])) { return true; } @@ -45,14 +44,14 @@ public function redirectDetailView( $addressId, $addressTitle, $pAddressRedirect $sanitizeTitle = $this->_wpAddressDetailUrl->getSanitizeTitle($addressTitle); $isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; $newUrl = $this->_wpAddressDetailUrl->getUrlWithAddressTitle($addressId, $addressTitle, $oldUrl, $isUrlHaveTitle, $pAddressRedirection); - if ( $newUrl !== $oldUrl ) { + if ($newUrl !== $oldUrl) { $isNewUrlValid = $this->checkNewUrlIsValid( - array_filter( explode( '/', $newUrl ) ), - array_filter( explode( '/', $oldUrl ) ) + array_filter(explode('/', $newUrl)), + array_filter(explode('/', $oldUrl)) ); - if ( $isNewUrlValid ) { - $this->_wpRedirectWrapper->redirect( $newUrl ); + if ($isNewUrlValid) { + $this->_wpRedirectWrapper->redirect($newUrl); } } } @@ -64,33 +63,33 @@ public function redirectDetailView( $addressId, $addressTitle, $pAddressRedirect public function checkUrlIsMatchRule() { - $uri = $this->getUri(); - $uriToArray = explode( '/', $uri ); - array_pop( $uriToArray ); - $pagePath = implode( '/', array_filter( $uriToArray ) ); + $uri = $this->getUri(); + $uriToArray = explode('/', $uri); + array_pop($uriToArray); + $pagePath = implode('/', array_filter($uriToArray)); //Check pass rule and has Unique ID - preg_match( '/^(' . preg_quote( $pagePath, - '/' ) . ')\/([0-9]+)(-([^$]+)?)?\/?$/', $uri, $matches ); + preg_match('/^(' . preg_quote($pagePath, + '/') . ')\/([0-9]+)(-([^$]+)?)?\/?$/', $uri, $matches); return $matches; } /** - * @param array $newUrlArr - * @param array $oldUrlArr + * @param array $newUrlArr + * @param array $oldUrlArr * * @return bool */ - public function checkNewUrlIsValid( array $newUrlArr, array $oldUrlArr ) + public function checkNewUrlIsValid(array $newUrlArr, array $oldUrlArr) { - if ( end( $newUrlArr ) !== end( $oldUrlArr ) ) { - array_pop( $newUrlArr ); - array_pop( $oldUrlArr ); + if (end($newUrlArr) !== end($oldUrlArr)) { + array_pop($newUrlArr); + array_pop($oldUrlArr); - return empty( array_diff( $newUrlArr, $oldUrlArr ) ); + return empty(array_diff($newUrlArr, $oldUrlArr)); } return false; diff --git a/tests/TestClassAddressDetailUrl.php b/tests/TestClassAddressDetailUrl.php new file mode 100644 index 000000000..626efa5e7 --- /dev/null +++ b/tests/TestClassAddressDetailUrl.php @@ -0,0 +1,209 @@ +. + * + */ + +declare (strict_types=1); + + +namespace onOffice\tests; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\WPlugin\Controller\AddressDetailUrl; +use onOffice\WPlugin\WP\WPQueryWrapper; +use WP_UnitTestCase; + +class TestClassAddressDetailUrl + extends WP_UnitTestCase +{ + + /** @var Container */ + private $_pContainer; + + /** + * @before + */ + public function prepare() + { + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + $this->_pContainer = $pContainerBuilder->build(); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testUrlWithFolder() + { + $addressId = 123; + + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/'; + $expectedUrl = $url.$addressId; + + $this->assertEquals($expectedUrl, $pInstance->createAddressDetailLink($url, $addressId)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testUrlWithGetParameter() + { + $addressId = 123; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/?lang=en'; + $expectedUrl = 'https://www.onoffice.de/detail/123?lang=en'; + + $this->assertEquals($expectedUrl, $pInstance->createAddressDetailLink($url, $addressId)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testUrlWithTitle() + { + add_option('onoffice-address-detail-view-showInfoUserUrl', true); + $addressId = 123; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/'; + $title = 'vorname name company'; + $expectedUrl = 'https://www.onoffice.de/detail/123-vorname-name-company'; + + $this->assertEquals($expectedUrl, $pInstance->createAddressDetailLink($url, $addressId, $title)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testUrlWithTitleNotSetOptionShowUrlAndTitle() + { + add_option('onoffice-address-detail-view-showInfoUserUrl', false); + + $addressId = 123; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/'; + $title = 'Human readable URLs showing ID and title'; + $expectedUrl = 'https://www.onoffice.de/detail/123'; + + $this->assertEquals($expectedUrl, $pInstance->createAddressDetailLink($url, $addressId, $title)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testUrlWithTitleAndParameter() + { + add_option('onoffice-address-detail-view-showInfoUserUrl', true); + $addressId = 123; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/?lang=en'; + $title = 'String Slug'; + $expectedUrl = 'https://www.onoffice.de/detail/123-string-slug?lang=en'; + + $this->assertEquals($expectedUrl, $pInstance->createAddressDetailLink($url, $addressId, $title)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testGetUrlWithFilterTrueAndEnableOptionShowTitleWithParameter() + { + $pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', true); + add_option('onoffice-address-detail-view-showInfoUserUrl', true); + $addressId = 123; + $title = 'test-title'; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/123?lang=en'; + $expectedUrl = 'https://www.onoffice.de/detail/123-test-title?lang=en'; + $this->assertEquals($expectedUrl, $pInstance->getUrlWithAddressTitle($addressId, $title, $url, false, $pAddressRedirection)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testGetUrlWithFilterTrueAndDisableOptionShowTitleWithParameter() + { + $pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', true); + add_option('onoffice-address-detail-view-showInfoUserUrl', false); + $addressId = 123; + $title = 'test-title'; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + $url = 'https://www.onoffice.de/detail/123?lang=en'; + $expectedUrl = 'https://www.onoffice.de/detail/123?lang=en'; + $this->assertEquals($expectedUrl, $pInstance->getUrlWithAddressTitle($addressId, $title, $url, false, $pAddressRedirection)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testGetUrlWithFilterFalseAndDisableOptionShowTitleWithParameter() + { + $pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', false); + add_option('onoffice-address-detail-view-showInfoUserUrl', false); + $addressId = 123; + $title = 'test-title'; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + + $urlHasTitleWithoutParamster = 'https://www.onoffice.de/detail/123'; + $expectedUrlHasTitleWithoutParamster = 'https://www.onoffice.de/detail/123'; + $this->assertEquals($expectedUrlHasTitleWithoutParamster, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlHasTitleWithoutParamster, false, $pAddressRedirection)); + + $urlWithoutTitle = 'https://www.onoffice.de/detail/123?lang=en'; + $expectedUrlWithoutTitle = 'https://www.onoffice.de/detail/123?lang=en'; + $this->assertEquals($expectedUrlWithoutTitle, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlWithoutTitle, false, $pAddressRedirection)); + } + + /** + * @throws \DI\DependencyException + * @throws \DI\NotFoundException + */ + public function testGetUrlWithFilterFalseAndEnableOptionShowTitleWithParameter() + { + $pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', false); + add_option('onoffice-address-detail-view-showInfoUserUrl', true); + $addressId = 123; + $title = 'test-title'; + $pInstance = $this->_pContainer->get(AddressDetailUrl::class); + + $urlHasTitleWithoutParamster = 'https://www.onoffice.de/detail/123'; + $expectedUrlHasTitleWithoutParamster = 'https://www.onoffice.de/detail/123-test-title'; + $this->assertEquals($expectedUrlHasTitleWithoutParamster, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlHasTitleWithoutParamster, false, $pAddressRedirection)); + + $urlWithoutTitle = 'https://www.onoffice.de/detail/123?lang=en'; + $expectedUrlWithoutTitle = 'https://www.onoffice.de/detail/123?lang=en'; + $this->assertEquals($expectedUrlWithoutTitle, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlWithoutTitle, false, $pAddressRedirection)); + + $urlHasTitleHasParameter = 'https://www.onoffice.de/detail/123-test-title?lang=en'; + $expectedUrlHasTitleHasParameter = 'https://www.onoffice.de/detail/123-test-title?lang=en'; + $this->assertEquals($expectedUrlHasTitleHasParameter, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlHasTitleHasParameter, true, $pAddressRedirection)); + + $urlHasTitle = 'https://www.onoffice.de/detail/123-test-title?lang=en'; + $expectedUrlHasTitle = 'https://www.onoffice.de/detail/123-test-title?lang=en'; + $this->assertEquals($expectedUrlHasTitle, $pInstance->getUrlWithAddressTitle($addressId, $title, $urlHasTitle, true, $pAddressRedirection)); + } +} diff --git a/tests/TestClassAddressIdRequestGuard.php b/tests/TestClassAddressIdRequestGuard.php new file mode 100644 index 000000000..1571664a0 --- /dev/null +++ b/tests/TestClassAddressIdRequestGuard.php @@ -0,0 +1,253 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\tests; + +use DI\Container; +use DI\ContainerBuilder; +use Generator; +use onOffice\SDK\onOfficeSDK; +use onOffice\WPlugin\ArrayContainerEscape; +use onOffice\WPlugin\Controller\AddressDetailUrl; +use onOffice\WPlugin\DataView\DataAddressDetailView; +use onOffice\WPlugin\AddressDetail; +use onOffice\WPlugin\Factory\AddressListFactory; +use onOffice\WPlugin\Record\AddressIdRequestGuard; +use onOffice\WPlugin\SDKWrapper; +use WP_UnitTestCase; + +/** + * + */ + +class TestClassAddressIdRequestGuard + extends WP_UnitTestCase +{ + + /** @var Container */ + private $_pContainer = null; + + /** + * @before + */ + public function prepare() + { + $switchLocale = 'DEU'; + $pSDKWrapperMocker = new SDKWrapperMocker(); + $pSDKWrapperMocker->addResponseByParameters + (onOfficeSDK::ACTION_ID_READ, 'address', '3', [ + 'data' => ['Vorname', 'Name', 'Zusatz1'], + 'outputlanguage' => $switchLocale + ], null, $this->prepareMockerForAddressDetailOne()); + + $pSDKWrapperMocker->addResponseByParameters + (onOfficeSDK::ACTION_ID_READ, 'address', '9', [ + 'data' => ['Vorname', 'Name', 'Zusatz1'], + 'outputlanguage' => $switchLocale + ], null, $this->prepareMockerForAddressDetailTwo()); + + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); + $this->_pContainer = $pContainerBuilder->build(); + $this->_pContainer->set(SDKWrapper::class, $pSDKWrapperMocker); + } + + /** + * + * @dataProvider dataProvider + * + * @param int $addressId + * @param bool|array $iterator + * @param bool $result + * + */ + + public function testIsValid(int $addressId, $iterator, bool $result) + { + $pAddressDetailFactory = $this->getMockBuilder(AddressListFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $pAddressDetail = $this->getMockBuilder(AddressDetail::class) + ->setConstructorArgs([new DataAddressDetailView()]) + ->setMethods(['loadSingleAddress', 'getRows']) + ->getMock(); + $pAddressDetail + ->expects($this->once())->method('getRows') + ->will($this->returnValue($iterator)); + $pAddressDetailFactory->method('createAddressDetail')->will($this->returnValue($pAddressDetail)); + $pSubject = new AddressIdRequestGuard($pAddressDetailFactory); + $this->assertEquals($result, $pSubject->isValid($addressId)); + } + + + /** + * + * @dataProvider dataProvider + * + * @param int $addressId + * @param bool|array $iterator + * @param bool $result + * + */ + public function testCreateAddressDetailLinkForSwitchLanguageWPML(int $addressId, $iterator, bool $result) + { + add_option('onoffice-address-detail-view-showInfoUserUrl', true); + $this->run_activate_plugin_for_test( 'sitepress-multilingual-cms/sitepress.php' ); + add_filter('wpml_active_languages', function () { + return [ + 'en' => ['default_locale' => 'en_US'], + 'de' => ['default_locale' => 'de_DE'], + ]; + }); + $data = $iterator[$addressId]; + + $url = 'https://www.onoffice.de/detail/'; + $title = $data ? '-' . $data['Vorname'] . '-' . $data['Name'] . '-' . $data['Zusatz1'] : ''; + $oldUrl = 'https://www.onoffice.de/detail/' . $addressId . $title . '/'; + $expectedUrl = 'https://www.onoffice.de/detail/' . $addressId . $title . '/'; + + $pAddressDetailFactory = $this->getMockBuilder(AddressListFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $pAddressDetail = $this->getMockBuilder(AddressDetail::class) + ->setConstructorArgs([new DataAddressDetailView()]) + ->setMethods(['loadSingleAddress', 'getRows']) + ->getMock(); + $pAddressDetail->expects($this->once())->method('getRows') + ->will($this->returnValue($iterator)); + + $pAddressDetailFactory->method('createAddressDetail')->will($this->returnValue($pAddressDetail)); + $pSubject = new AddressIdRequestGuard($pAddressDetailFactory, $this->_pContainer); + $pSubject->isValid($addressId); + + $pAddressDetailUrl = new AddressDetailUrl(); + + $result = $pSubject->createAddressDetailLinkForSwitchLanguageWPML($url, $addressId, $pAddressDetailUrl, $oldUrl, 'en_US'); + + $this->assertEquals($expectedUrl, $result); + } + + /** + * + * @return Generator + * + */ + + public function dataProvider(): Generator + { + yield from [ + [3, [3 => new ArrayContainerEscape(['Id' => 3, 'Vorname' => 'testvorname-3', 'Name' => 'testname-3', 'Zusatz1' => 'company-3'])], true], + [9, [9 => new ArrayContainerEscape(['Id' => 9, 'Vorname' => 'testvorname-9', 'Name' => 'testname-9', 'Zusatz1' => 'company-9'])], true], + ]; + } + + /** + * @param $plugin + * @return null + */ + private function run_activate_plugin_for_test($plugin) { + $current = get_option('active_plugins'); + $plugin = plugin_basename(trim($plugin)); + + if (!in_array($plugin, $current)) { + $current[] = $plugin; + sort($current); + do_action('activate_plugin', trim($plugin)); + update_option('active_plugins', $current); + do_action('activate_' . trim($plugin)); + do_action('activated_plugin', trim($plugin)); + } + + return null; + } + + /** + * + */ + private function prepareMockerForAddressDetailOne() + { + $response = [ + 'actionid' => 'urn:onoffice-de-ns:smart:2.5:smartml:action:read', + 'resourceid' => '3', + 'resourcetype' => 'address', + 'identifier' => '', + 'data' => [ + 'meta' => [ + 'cntabsolute' => 1, + ], + 'records' => [ + 0 => [ + 'id' => 3, + 'type' => 'address', + 'elements' => [ + "Vorname" => "testvorname-3", + "Name" => "testname-3", + "Zusatz1" => "company-3" + ], + ], + ], + ], + 'status' => [ + 'errorcode' => 0, + 'message' => 'OK', + ], + ]; + + return $response; + } + + /** + * + */ + private function prepareMockerForAddressDetailTwo() + { + $response = [ + 'actionid' => 'urn:onoffice-de-ns:smart:2.5:smartml:action:read', + 'resourceid' => '9', + 'resourcetype' => 'address', + 'identifier' => '', + 'data' => [ + 'meta' => [ + 'cntabsolute' => 1, + ], + 'records' => [ + 0 => [ + 'id' => 9, + 'type' => 'address', + 'elements' => [ + "Vorname" => "testvorname-9", + "Name" => "testname-9", + "Zusatz1" => "company-9" + ], + ], + ], + ], + 'status' => [ + 'errorcode' => 0, + 'message' => 'OK', + ], + ]; + + return $response; + } +} \ No newline at end of file diff --git a/tests/TestClassAddressList.php b/tests/TestClassAddressList.php index 711e290db..8c2d73c92 100644 --- a/tests/TestClassAddressList.php +++ b/tests/TestClassAddressList.php @@ -48,6 +48,9 @@ use onOffice\WPlugin\API\DataViewToAPI\DataListViewAddressToAPIParameters; use WP_UnitTestCase; use function json_decode; +use onOffice\WPlugin\DataView\DataAddressDetailView; +use onOffice\WPlugin\DataView\DataAddressDetailViewHandler; +use onOffice\WPlugin\Controller\AddressListEnvironmentDefault; /** * @@ -86,6 +89,9 @@ class TestClassAddressList private $_pAddressList = null; + /** @var AddressListEnvironment */ + private $_pEnvironment = null; + /** * * @before @@ -144,30 +150,30 @@ public function prepare() ]; $addressParametersWithFormatDetail = [ - 'recordids' => [13,37], - 'data' => ['contactCategory', 'Vorname', 'Name', 'Zusatz1', 'branch', 'communityOfHeirs', 'communityOfOwners', 'umbrellaOrganization', 'association', 'institution', 'department'], - 'outputlanguage' => "ENG", - 'formatoutput' => false, + 'recordids' => [13,37], + 'data' => ['contactCategory', 'Vorname', 'Name', 'Zusatz1', 'branch', 'communityOfHeirs', 'communityOfOwners', 'umbrellaOrganization', 'association', 'institution', 'department'], + 'outputlanguage' => "ENG", + 'formatoutput' => false, ]; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $parameters, null, $response); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $parameters, null, $response); $pSDKWrapper->addResponseByParameters (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); - $addressParametersWithoutFormat['data'][] = 'imageUrl'; + $addressParametersWithoutFormat['data'][] = 'imageUrl'; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); $addressParametersWithFormat['data'][] = 'imageUrl'; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormatDetail, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormatDetail, null, $responseRaw); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_GET, 'idsfromrelation', '', $parametersRelation, null, $responseRelation); + (onOfficeSDK::ACTION_ID_GET, 'idsfromrelation', '', $parametersRelation, null, $responseRelation); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'estate', '', $parametersEstatesOfAddress, null, $responseEstatesOfAddress); + (onOfficeSDK::ACTION_ID_READ, 'estate', '', $parametersEstatesOfAddress, null, $responseEstatesOfAddress); $pMockViewFieldModifierHandler = $this->getMockBuilder(ViewFieldModifierHandler::class) ->setMethods(['processRecord', 'getAllAPIFields']) @@ -211,11 +217,11 @@ public function prepare() $pFilterBuilderFactory = $pContainer->get(FilterBuilderInputVariablesFactory::class); $pCompoundFieldsFilter = $pContainer->get(CompoundFieldsFilter::class); $pFactory = $this->getMockBuilder(DefaultFilterBuilderListViewAddressFactory::class) - ->setConstructorArgs([$pFieldsCollectionBuilderShort, $pCompoundFieldsFilter, $pFilterBuilderFactory]) - ->getMock(); + ->setConstructorArgs([$pFieldsCollectionBuilderShort, $pCompoundFieldsFilter, $pFilterBuilderFactory]) + ->getMock(); $pDataListViewAddressToAPIParameters = new DataListViewAddressToAPIParameters($pFactory); - $pMockConfig = $this->getMockBuilder(AddressListEnvironment::class)->getMock(); + $pMockConfig = $this->getMockBuilder(AddressListEnvironmentDefault::class)->getMock(); $pMockConfig->method('getSDKWrapper')->will($this->returnValue($pSDKWrapper)); $pMockConfig->method('getViewFieldModifierHandler') ->will($this->returnValue($pMockViewFieldModifierHandler)); @@ -224,8 +230,8 @@ public function prepare() $pMockConfig->method('getDataListViewAddressToAPIParameters')->will($this->returnValue($pDataListViewAddressToAPIParameters)); $pFieldsCollectionBuilderMock = $this->getMockBuilder(FieldsCollectionBuilderShort::class) - ->setConstructorArgs([new Container()]) - ->getMock(); + ->setConstructorArgs([new Container()]) + ->getMock(); $pFieldsCollectionNewFields = new FieldsCollection; $pFieldsCollectionNewFields->addField(new Field('KdNr', onOfficeSDK::MODULE_ADDRESS, 'Kundennummer')); @@ -240,8 +246,9 @@ public function prepare() })); $pMockConfig->method('getFieldsCollectionBuilderShort')->willReturn($pFieldsCollectionBuilderMock); + $this->_pEnvironment = $pMockConfig; - $this->_pAddressList = new AddressList(null, $pMockConfig); + $this->_pAddressList = new AddressList(null, $this->_pEnvironment); } @@ -352,6 +359,38 @@ public function testGetFieldType() $this->_pAddressList->getFieldType('HerkunftKontakt')); } + /** + * + */ + + public function testGetAddressLink() + { + global $wp_filter; + $this->_pAddressList->loadAddresses(); + $this->_pAddressList->getRows(); + $pDataDetailView = new DataAddressDetailView(); + $pDataDetailViewHandler = $this->getMockBuilder(DataAddressDetailViewHandler::class) + ->disableOriginalConstructor() + ->setMethods(['getAddressDetailView']) + ->getMock(); + $pDataDetailViewHandler->method('getAddressDetailView')->willReturn($pDataDetailView); + $this->_pEnvironment->method('getDataAddressDetailViewHandler')->willReturn($pDataDetailViewHandler); + + $this->set_permalink_structure('/%postname%/'); + $savePostBackup = $wp_filter['save_post']; + + $wp_filter['save_post'] = new \WP_Hook; + $pWPPost = self::factory()->post->create_and_get([ + 'post_author' => 1, + 'post_content' => '[oo_address view="detail"]', + 'post_title' => 'Details', + 'post_type' => 'page', + ]); + $wp_filter['save_post'] = $savePostBackup; + $pDataDetailView->setPageId($pWPPost->ID); + + $this->assertEquals('http://example.org/details/15/', $this->_pAddressList->getAddressLink("15")); + } /** * @@ -511,6 +550,7 @@ private function getResponseRelation() return json_decode($responseStr, true); } + /** * * @return string diff --git a/tests/TestClassAddressRedirectIfOldUrl.php b/tests/TestClassAddressRedirectIfOldUrl.php new file mode 100644 index 000000000..e2102066d --- /dev/null +++ b/tests/TestClassAddressRedirectIfOldUrl.php @@ -0,0 +1,116 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\tests; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\WPlugin\Utility\AddressRedirector; + +class TestClassAddressRedirectIfOldUrl + extends \WP_UnitTestCase +{ + /** + * @var AddressRedirector + */ + private $_pRedirectIfOldUrl; + + /** @var Container */ + private $_pContainer; + + /** + * @before + */ + public function prepare() + { + global $wp; + $wp->request = 'address-detail-view/123'; + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions( ONOFFICE_DI_CONFIG_PATH ); + $this->_pContainer = $pContainerBuilder->build(); + $this->_pRedirectIfOldUrl = $this->_pContainer->get( AddressRedirector::class ); + $_SERVER['REQUEST_URI'] = '/address-detail-view/123'; + } + + /** + * + */ + public function stestInstance() + { + $this->assertInstanceOf( AddressRedirector::class, $this->_pRedirectIfOldUrl ); + } + + /** + * @covers \onOffice\WPlugin\Utility\AddressRedirector::getCurrentLink + */ + public function testGetCurrentLink() + { + $this->assertEquals( 'http://example.org/address-detail-view/123', $this->_pRedirectIfOldUrl->getCurrentLink() ); + } + + /** + * @covers \onOffice\WPlugin\Utility\AddressRedirector::getUri + */ + public function testGetUri() + { + $this->assertEquals( 'address-detail-view/123', $this->_pRedirectIfOldUrl->getUri() ); + } + + /** + * @covers \onOffice\WPlugin\Utility\AddressRedirector::redirectDetailView + */ + public function testRedirectDetailViewSameUrl() + { + add_option( 'onoffice-address-detail-view-showInfoUserUrl', true ); + global $wp; + $wp->request = 'address-detail-view/123-vorname-name-company'; + $_SERVER['REQUEST_URI'] = '/address-detail-view/123-vorname-name-company'; + $this->set_permalink_structure( '/%postname%/' ); + $this->assertNull($this->_pRedirectIfOldUrl->redirectDetailView(123, 'Vorname Name Company', true)); + } + + /** + * @covers \onOffice\WPlugin\Utility\AddressRedirector::redirectDetailView + */ + public function testRedirectDetailViewNotMatchRule() + { + global $wp; + $wp->request = 'address-detail-view/abc-123-vorname-name-company-url'; + $_SERVER['REQUEST_URI'] = '/address-detail-view/abc-123-vorname-name-company-url'; + $this->set_permalink_structure( '/%postname%/' ); + $this->assertTrue($this->_pRedirectIfOldUrl->redirectDetailView(123, 'Vorname Name Company', true)); + } + + + /** + * @return void + */ + + public function testRedirectDetailViewWithParrentPageAndUrlNotMatchRule() + { + global $wp; + $wp->request = 'e1/address-detail-view/abc-123-vorname-name-company-url'; + $_SERVER['REQUEST_URI'] = '/e1/address-detail-view/abc-123-vorname-name-company-url'; + $this->set_permalink_structure( '/%postname%/' ); + $this->assertTrue($this->_pRedirectIfOldUrl->redirectDetailView(123, 'Vorname Name Company', true)); + } +} diff --git a/tests/TestClassAddressViewFieldModifierTypeTitle.php b/tests/TestClassAddressViewFieldModifierTypeTitle.php new file mode 100644 index 000000000..3e3219e28 --- /dev/null +++ b/tests/TestClassAddressViewFieldModifierTypeTitle.php @@ -0,0 +1,143 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\tests; + +use onOffice\WPlugin\ViewFieldModifier\AddressViewFieldModifierTypeTitle; +use WP_UnitTestCase; + +/** + * + * @url http://www.onoffice.de + * @copyright 2003-2024, onOffice(R) GmbH + * + * @covers \onOffice\WPlugin\ViewFieldModifier\AddressViewFieldModifierTypeTitle + * + */ + +class TestClassAddressViewFieldModifierTypeTitle + extends WP_UnitTestCase +{ + /** @var array */ + private $_exampleRecord = [ + 'test1' => 'test', + 'test2' => 'test 2', + 'Vorname' => 'test Vorname 1', + 'Name' => 'test Name', + 'Zusatz1' => 'test Zusatz1' + ]; + + + /** + * + */ + + public function testApiFields() + { + $expectation = [ + 'test1', + 'test2', + 'Vorname', + 'Name', + 'Zusatz1' + ]; + + $pAddressViewFieldModifierTypeTitle = new AddressViewFieldModifierTypeTitle(['test1', 'test2']); + $apiFields = $pAddressViewFieldModifierTypeTitle->getAPIFields(); + $this->assertEquals($expectation, $apiFields); + } + + + /** + * + */ + + public function testVisibleFields() + { + $expectation = [ + 'test1', + 'test2', + 'Vorname', + 'Name', + 'Zusatz1' + ]; + + $pAddressViewFieldModifierTypeTitle = new AddressViewFieldModifierTypeTitle(['test1', 'test2']); + $visibleFields = $pAddressViewFieldModifierTypeTitle->getVisibleFields(); + $this->assertEquals($expectation, $visibleFields); + } + + + /** + * + */ + + public function testReduceRecord() + { + $pAddressViewFieldModifierTypeTitle = new AddressViewFieldModifierTypeTitle(['test1', 'test2']); + $record = $pAddressViewFieldModifierTypeTitle->reduceRecord($this->_exampleRecord); + // this won't do any change + $this->assertEquals($this->_exampleRecord, $record); + } + + /** + * @covers onOffice\WPlugin\ViewFieldModifier\AddressViewFieldModifierTypeTitle::getAPICustomFields + */ + public function testApiCustomFields() + { + $expectation = [ + 'Anrede', + 'Vorname', + 'Name', + 'Zusatz1', + 'Email', + 'Telefon1', + 'Telefax1', + ]; + + $pAddressViewFieldModifierTypeTitle = new AddressViewFieldModifierTypeTitle(['test1', 'test2']); + $apiCustomFields = $pAddressViewFieldModifierTypeTitle->getAPICustomFields(); + $this->assertEquals($expectation, $apiCustomFields); + } + + /** + * @covers onOffice\WPlugin\ViewFieldModifier\AddressViewFieldModifierTypeTitle::getVisibleCustomFields + */ + + public function testVisibleCustomFields() + { + $expectation = [ + 'Anrede', + 'Vorname', + 'Name', + 'Zusatz1', + 'Email', + 'Telefon1', + 'Telefax1', + ]; + + $pAddressViewFieldModifierTypeTitle = new AddressViewFieldModifierTypeTitle(['test1', 'test2']); + $visibleCustomFields = $pAddressViewFieldModifierTypeTitle->getVisibleCustomFields(); + $this->assertEquals($expectation, $visibleCustomFields); + } +} From 8bb8917ce5a216ebaa8f1f3f5d106430281ab431 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Tue, 15 Oct 2024 10:19:58 +0700 Subject: [PATCH 3/8] 53667 refactor code --- plugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.php b/plugin.php index 9157353a3..1057c4b16 100644 --- a/plugin.php +++ b/plugin.php @@ -415,7 +415,7 @@ function custom_cron_schedules($schedules) { add_action('parse_request', function(WP $pWP) use ($pDI, $pAddressRedirection) { $addressId = $pWP->query_vars['address_id'] ?? ''; - /** @var AddressIdRequestGuard $pEstateIdGuard */ + /** @var AddressIdRequestGuard $pAddressIdGuard */ $pAddressIdGuard = $pDI->get(AddressIdRequestGuard::class); if ($addressId !== '') { @@ -426,7 +426,7 @@ function custom_cron_schedules($schedules) { include(get_query_template('404')); die(); } - $pAddressIdGuard->addressDetailUrlChecker( $addressId, $pDI->get( AddressRedirector::class ), $pAddressRedirection); + $pAddressIdGuard->addressDetailUrlChecker($addressId, $pDI->get(AddressRedirector::class), $pAddressRedirection); } }); From b2076ce6f8b5d3956f824378eebf15e485fdca54 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Tue, 15 Oct 2024 12:58:49 +0700 Subject: [PATCH 4/8] 53667 update unit test --- tests/TestClassAddressDetailUrl.php | 6 ++-- tests/TestClassAddressList.php | 35 +++++++++++----------- tests/TestClassAddressRedirectIfOldUrl.php | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/tests/TestClassAddressDetailUrl.php b/tests/TestClassAddressDetailUrl.php index 626efa5e7..28d788962 100644 --- a/tests/TestClassAddressDetailUrl.php +++ b/tests/TestClassAddressDetailUrl.php @@ -80,7 +80,7 @@ public function testUrlWithGetParameter() * @throws \DI\DependencyException * @throws \DI\NotFoundException */ - public function testUrlWithTitle() + public function testAddressDetailUrl() { add_option('onoffice-address-detail-view-showInfoUserUrl', true); $addressId = 123; @@ -96,7 +96,7 @@ public function testUrlWithTitle() * @throws \DI\DependencyException * @throws \DI\NotFoundException */ - public function testUrlWithTitleNotSetOptionShowUrlAndTitle() + public function testAddressDetailUrlNotSetOptionShowUrlAndTitle() { add_option('onoffice-address-detail-view-showInfoUserUrl', false); @@ -113,7 +113,7 @@ public function testUrlWithTitleNotSetOptionShowUrlAndTitle() * @throws \DI\DependencyException * @throws \DI\NotFoundException */ - public function testUrlWithTitleAndParameter() + public function testAddressDetailUrlAndParameter() { add_option('onoffice-address-detail-view-showInfoUserUrl', true); $addressId = 123; diff --git a/tests/TestClassAddressList.php b/tests/TestClassAddressList.php index 8c2d73c92..8015affdc 100644 --- a/tests/TestClassAddressList.php +++ b/tests/TestClassAddressList.php @@ -150,30 +150,30 @@ public function prepare() ]; $addressParametersWithFormatDetail = [ - 'recordids' => [13,37], - 'data' => ['contactCategory', 'Vorname', 'Name', 'Zusatz1', 'branch', 'communityOfHeirs', 'communityOfOwners', 'umbrellaOrganization', 'association', 'institution', 'department'], - 'outputlanguage' => "ENG", - 'formatoutput' => false, + 'recordids' => [13,37], + 'data' => ['contactCategory', 'Vorname', 'Name', 'Zusatz1', 'branch', 'communityOfHeirs', 'communityOfOwners', 'umbrellaOrganization', 'association', 'institution', 'department'], + 'outputlanguage' => "ENG", + 'formatoutput' => false, ]; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $parameters, null, $response); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $parameters, null, $response); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); $addressParametersWithoutFormat['data'][] = 'imageUrl'; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); $addressParametersWithFormat['data'][] = 'imageUrl'; $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormat, null, $responseRaw); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormatDetail, null, $responseRaw); + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithFormatDetail, null, $responseRaw); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_GET, 'idsfromrelation', '', $parametersRelation, null, $responseRelation); + (onOfficeSDK::ACTION_ID_GET, 'idsfromrelation', '', $parametersRelation, null, $responseRelation); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'estate', '', $parametersEstatesOfAddress, null, $responseEstatesOfAddress); + (onOfficeSDK::ACTION_ID_READ, 'estate', '', $parametersEstatesOfAddress, null, $responseEstatesOfAddress); $pMockViewFieldModifierHandler = $this->getMockBuilder(ViewFieldModifierHandler::class) ->setMethods(['processRecord', 'getAllAPIFields']) @@ -217,8 +217,8 @@ public function prepare() $pFilterBuilderFactory = $pContainer->get(FilterBuilderInputVariablesFactory::class); $pCompoundFieldsFilter = $pContainer->get(CompoundFieldsFilter::class); $pFactory = $this->getMockBuilder(DefaultFilterBuilderListViewAddressFactory::class) - ->setConstructorArgs([$pFieldsCollectionBuilderShort, $pCompoundFieldsFilter, $pFilterBuilderFactory]) - ->getMock(); + ->setConstructorArgs([$pFieldsCollectionBuilderShort, $pCompoundFieldsFilter, $pFilterBuilderFactory]) + ->getMock(); $pDataListViewAddressToAPIParameters = new DataListViewAddressToAPIParameters($pFactory); $pMockConfig = $this->getMockBuilder(AddressListEnvironmentDefault::class)->getMock(); @@ -230,8 +230,8 @@ public function prepare() $pMockConfig->method('getDataListViewAddressToAPIParameters')->will($this->returnValue($pDataListViewAddressToAPIParameters)); $pFieldsCollectionBuilderMock = $this->getMockBuilder(FieldsCollectionBuilderShort::class) - ->setConstructorArgs([new Container()]) - ->getMock(); + ->setConstructorArgs([new Container()]) + ->getMock(); $pFieldsCollectionNewFields = new FieldsCollection; $pFieldsCollectionNewFields->addField(new Field('KdNr', onOfficeSDK::MODULE_ADDRESS, 'Kundennummer')); @@ -365,6 +365,7 @@ public function testGetFieldType() public function testGetAddressLink() { + add_option('onoffice-address-detail-view-showInfoUserUrl', true); global $wp_filter; $this->_pAddressList->loadAddresses(); $this->_pAddressList->getRows(); @@ -389,7 +390,7 @@ public function testGetAddressLink() $wp_filter['save_post'] = $savePostBackup; $pDataDetailView->setPageId($pWPPost->ID); - $this->assertEquals('http://example.org/details/15/', $this->_pAddressList->getAddressLink("15")); + $this->assertEquals('http://example.org/details/13-fred-firestone/', $this->_pAddressList->getAddressLink("13")); } /** diff --git a/tests/TestClassAddressRedirectIfOldUrl.php b/tests/TestClassAddressRedirectIfOldUrl.php index e2102066d..002d46a90 100644 --- a/tests/TestClassAddressRedirectIfOldUrl.php +++ b/tests/TestClassAddressRedirectIfOldUrl.php @@ -1,7 +1,7 @@ Date: Tue, 15 Oct 2024 13:00:02 +0700 Subject: [PATCH 5/8] 53667 refactor code --- tests/TestClassAddressList.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestClassAddressList.php b/tests/TestClassAddressList.php index 8015affdc..b0fb093d5 100644 --- a/tests/TestClassAddressList.php +++ b/tests/TestClassAddressList.php @@ -159,8 +159,8 @@ public function prepare() $pSDKWrapper->addResponseByParameters (onOfficeSDK::ACTION_ID_READ, 'address', '', $parameters, null, $response); $pSDKWrapper->addResponseByParameters - (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); - $addressParametersWithoutFormat['data'][] = 'imageUrl'; + (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); + $addressParametersWithoutFormat['data'][] = 'imageUrl'; $pSDKWrapper->addResponseByParameters (onOfficeSDK::ACTION_ID_READ, 'address', '', $addressParametersWithoutFormat, null, $response); $pSDKWrapper->addResponseByParameters From a887c50ffa1293a86405e7e673a01489d36504b2 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Tue, 15 Oct 2024 17:02:47 +0700 Subject: [PATCH 6/8] 53667 update ubuntu v --- .github/workflows/unit-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 0f12ebe7c..b8d7aea9e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -15,7 +15,7 @@ jobs: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: [ubuntu-latest] + operating-system: [ubuntu-20.04] php-versions: ['7.4', '8.0'] name: Unittest on ${{ matrix.operating-system }} with PHP version ${{ matrix.php-versions }} steps: From b8627cadd7555f4673b3d82b3a1327998bac8bc9 Mon Sep 17 00:00:00 2001 From: andernath Date: Tue, 22 Oct 2024 10:49:37 +0200 Subject: [PATCH 7/8] Refactoring Redirector class for improved reusability p#119769 --- plugin.php | 5 +- .../Redirector}/AddressRedirector.php | 77 +++---------------- .../Redirector/EstateRedirector.php | 64 +++++++++++++++ plugin/Gui/AdminPageApiSettings.php | 2 +- plugin/Record/AddressIdRequestGuard.php | 2 +- plugin/Record/EstateIdRequestGuard.php | 8 +- plugin/Utility/Redirector.php | 55 ------------- tests/TestClassAddressRedirectIfOldUrl.php | 18 +---- ...hp => TestClassEstateRedirectIfOldUrl.php} | 33 ++------ tests/TestClassRedirector.php | 77 +++++++++++++++++++ 10 files changed, 168 insertions(+), 173 deletions(-) rename plugin/{Utility => Controller/Redirector}/AddressRedirector.php (53%) create mode 100644 plugin/Controller/Redirector/EstateRedirector.php rename tests/{TestClassRedirectIfOldUrl.php => TestClassEstateRedirectIfOldUrl.php} (79%) create mode 100644 tests/TestClassRedirector.php diff --git a/plugin.php b/plugin.php index aca1c313b..2a7da53d4 100644 --- a/plugin.php +++ b/plugin.php @@ -67,7 +67,8 @@ use onOffice\WPlugin\WP\WPQueryWrapper; use onOffice\WPlugin\ScriptLoader\IncludeFileModel; use onOffice\WPlugin\Record\AddressIdRequestGuard; -use onOffice\WPlugin\Utility\AddressRedirector; +use onOffice\WPlugin\Controller\Redirector\AddressRedirector; +use onOffice\WPlugin\Controller\Redirector\EstateRedirector; use onOffice\WPlugin\Controller\AddressDetailUrl; const DEFAULT_LIMIT_CHARACTER_TITLE = 60; @@ -407,7 +408,7 @@ function custom_cron_schedules($schedules) { include(get_query_template('404')); die(); } - $pEstateIdGuard->estateDetailUrlChecker( $estateId, $pDI->get( Redirector::class ), $pEstateRedirection); + $pEstateIdGuard->estateDetailUrlChecker( $estateId, $pDI->get( EstateRedirector::class ), $pEstateRedirection); } }); diff --git a/plugin/Utility/AddressRedirector.php b/plugin/Controller/Redirector/AddressRedirector.php similarity index 53% rename from plugin/Utility/AddressRedirector.php rename to plugin/Controller/Redirector/AddressRedirector.php index a03af376a..ecddaf676 100644 --- a/plugin/Utility/AddressRedirector.php +++ b/plugin/Controller/Redirector/AddressRedirector.php @@ -1,8 +1,9 @@ _wpAddressDetailUrl = $addressDetailUrl; $this->_wpRedirectWrapper = $redirectWrapper; - } + $this->_redirector = new Redirector(); + } /** @@ -35,17 +40,17 @@ public function __construct(AddressDetailUrl $addressDetailUrl, WPRedirectWrappe public function redirectDetailView(int $addressId, string $addressTitle, bool $pAddressRedirection) { - $matches = $this->checkUrlIsMatchRule(); + $matches = $this->_redirector->checkUrlIsMatchRule(); if (empty($matches[2])) { return true; } - $oldUrl = $this->getCurrentLink(); + $oldUrl = $this->_redirector->getCurrentLink(); $sanitizeTitle = $this->_wpAddressDetailUrl->getSanitizeTitle($addressTitle); $isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; $newUrl = $this->_wpAddressDetailUrl->getUrlWithAddressTitle($addressId, $addressTitle, $oldUrl, $isUrlHaveTitle, $pAddressRedirection); if ($newUrl !== $oldUrl) { - $isNewUrlValid = $this->checkNewUrlIsValid( + $isNewUrlValid = $this->_redirector->checkNewUrlIsValid( array_filter(explode('/', $newUrl)), array_filter(explode('/', $oldUrl)) ); @@ -55,66 +60,4 @@ public function redirectDetailView(int $addressId, string $addressTitle, bool $p } } } - - - /** - * @return mixed - */ - - public function checkUrlIsMatchRule() - { - $uri = $this->getUri(); - $uriToArray = explode('/', $uri); - array_pop($uriToArray); - $pagePath = implode('/', array_filter($uriToArray)); - - //Check pass rule and has Unique ID - preg_match('/^(' . preg_quote($pagePath, - '/') . ')\/([0-9]+)(-([^$]+)?)?\/?$/', $uri, $matches); - - return $matches; - } - - - /** - * @param array $newUrlArr - * @param array $oldUrlArr - * - * @return bool - */ - - public function checkNewUrlIsValid(array $newUrlArr, array $oldUrlArr) - { - if (end($newUrlArr) !== end($oldUrlArr)) { - array_pop($newUrlArr); - array_pop($oldUrlArr); - - return empty(array_diff($newUrlArr, $oldUrlArr)); - } - - return false; - } - - - /** - * @return mixed - */ - - public function getUri() - { - global $wp; - - return $wp->request; - } - - - /** - * @return string - */ - - public function getCurrentLink(): string - { - global $wp; - return home_url(add_query_arg($_GET, $wp->request)); - } } diff --git a/plugin/Controller/Redirector/EstateRedirector.php b/plugin/Controller/Redirector/EstateRedirector.php new file mode 100644 index 000000000..e4fa4fa4f --- /dev/null +++ b/plugin/Controller/Redirector/EstateRedirector.php @@ -0,0 +1,64 @@ +_wpEstateDetailUrl = $estateDetailUrl; + $this->_wpRedirectWrapper = $redirectWrapper; + $this->_redirector = new Redirector(); + } + + + /** + * @param $estateId + * @param $estateTitle + * @param $pEstateRedirection + * @return bool|void + */ + + public function redirectDetailView( $estateId, $estateTitle, $pEstateRedirection ) + { + $matches = $this->_redirector->checkUrlIsMatchRule(); + if ( empty( $matches[2] ) ) { + return true; + } + + $oldUrl = $this->_redirector->getCurrentLink(); + $sanitizeTitle = $this->_wpEstateDetailUrl->getSanitizeTitle($estateTitle); + $isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; + $newUrl = $this->_wpEstateDetailUrl->getUrlWithEstateTitle($estateId, $estateTitle, $oldUrl, $isUrlHaveTitle, $pEstateRedirection); + + if ( $newUrl !== $oldUrl ) { + $isNewUrlValid = $this->_redirector->checkNewUrlIsValid( + array_filter( explode( '/', $newUrl ) ), + array_filter( explode( '/', $oldUrl ) ) + ); + + if ( $isNewUrlValid ) { + $this->_wpRedirectWrapper->redirect( $newUrl ); + } + } + } +} diff --git a/plugin/Gui/AdminPageApiSettings.php b/plugin/Gui/AdminPageApiSettings.php index fcef222de..96bb3606f 100644 --- a/plugin/Gui/AdminPageApiSettings.php +++ b/plugin/Gui/AdminPageApiSettings.php @@ -492,7 +492,7 @@ private function addFormModelDetailView(string $pageSlug) $pInputModelShowTitleUrl->setDescriptionTextHTML(__('If this checkbox is selected, the title of the property will be part of the URLs of the detail views. The title is placed after the record number, e.g. /1234-nice-location-with-view. No more than the first five words of the title are used.', 'onoffice-for-wp-websites')); $groupSlugView = 'onoffice-address-detail-view'; - $showTitleInUrl = __('Enable "Speaking" URL in Address Detail', 'onoffice-for-wp-websites'); + $showTitleInUrl = __('Show Name and Company in the URL', 'onoffice-for-wp-websites'); $pInputModelShowInfoContactUrl = new InputModelOption($groupSlugView, 'showInfoUserUrl', $showTitleInUrl, InputModelOption::SETTING_TYPE_BOOLEAN); $pInputModelShowInfoContactUrl->setHtmlType(InputModelOption::HTML_TYPE_CHECKBOX); diff --git a/plugin/Record/AddressIdRequestGuard.php b/plugin/Record/AddressIdRequestGuard.php index 6e9fc2078..00c7eace7 100644 --- a/plugin/Record/AddressIdRequestGuard.php +++ b/plugin/Record/AddressIdRequestGuard.php @@ -31,7 +31,7 @@ use onOffice\WPlugin\Factory\AddressListFactory; use onOffice\WPlugin\Language; use onOffice\WPlugin\SDKWrapper; -use onOffice\WPlugin\Utility\AddressRedirector; +use onOffice\WPlugin\Controller\Redirector\AddressRedirector; /** * diff --git a/plugin/Record/EstateIdRequestGuard.php b/plugin/Record/EstateIdRequestGuard.php index 3d3bdd406..e93963a17 100644 --- a/plugin/Record/EstateIdRequestGuard.php +++ b/plugin/Record/EstateIdRequestGuard.php @@ -32,8 +32,8 @@ use onOffice\WPlugin\Factory\EstateListFactory; use onOffice\WPlugin\Language; use onOffice\WPlugin\SDKWrapper; +use onOffice\WPlugin\Controller\Redirector\EstateRedirector; use onOffice\WPlugin\ViewFieldModifier\EstateViewFieldModifierTypes; -use onOffice\WPlugin\Utility\Redirector; /** * @@ -101,15 +101,15 @@ public function isValid(int $estateId): bool /** * * @param int $estateId - * @param Redirector $pRedirector + * @param EstateRedirector $pEstateRedirector * @param bool $pEstateRedirection * * @return void */ - public function estateDetailUrlChecker( int $estateId, Redirector $pRedirector, bool $pEstateRedirection ) { + public function estateDetailUrlChecker(int $estateId, EstateRedirector $pEstateRedirector, bool $pEstateRedirection ) { $estateTitle = $this->_estateData->getValue( 'objekttitel' ); - $pRedirector->redirectDetailView($estateId, $estateTitle, $pEstateRedirection); + $pEstateRedirector->redirectDetailView($estateId, $estateTitle, $pEstateRedirection); } /** diff --git a/plugin/Utility/Redirector.php b/plugin/Utility/Redirector.php index 9cde7ec07..1ac7b77d5 100644 --- a/plugin/Utility/Redirector.php +++ b/plugin/Utility/Redirector.php @@ -2,63 +2,8 @@ namespace onOffice\WPlugin\Utility; -use onOffice\WPlugin\Controller\EstateDetailUrl; -use onOffice\WPlugin\WP\WPPageWrapper; -use onOffice\WPlugin\WP\WPRedirectWrapper; - class Redirector { - /** @var EstateDetailUrl */ - private $_wpEstateDetailUrl; - - /** @var WPRedirectWrapper */ - private $_wpRedirectWrapper; - - - /** - * @param EstateDetailUrl $estateDetailUrl - * @param WPRedirectWrapper $redirectWrapper - */ - - public function __construct( EstateDetailUrl $estateDetailUrl, WPRedirectWrapper $redirectWrapper ) - { - $this->_wpEstateDetailUrl = $estateDetailUrl; - $this->_wpRedirectWrapper = $redirectWrapper; - } - - - /** - * @param $estateId - * @param $estateTitle - * @param $pEstateRedirection - * @return bool|void - */ - - public function redirectDetailView( $estateId, $estateTitle, $pEstateRedirection ) - { - $matches = $this->checkUrlIsMatchRule(); - if ( empty( $matches[2] ) ) { - return true; - } - - $oldUrl = $this->getCurrentLink(); - $sanitizeTitle = $this->_wpEstateDetailUrl->getSanitizeTitle($estateTitle); - $isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; - $newUrl = $this->_wpEstateDetailUrl->getUrlWithEstateTitle($estateId, $estateTitle, $oldUrl, $isUrlHaveTitle, $pEstateRedirection); - - if ( $newUrl !== $oldUrl ) { - $isNewUrlValid = $this->checkNewUrlIsValid( - array_filter( explode( '/', $newUrl ) ), - array_filter( explode( '/', $oldUrl ) ) - ); - - if ( $isNewUrlValid ) { - $this->_wpRedirectWrapper->redirect( $newUrl ); - } - } - } - - /** * @return mixed */ diff --git a/tests/TestClassAddressRedirectIfOldUrl.php b/tests/TestClassAddressRedirectIfOldUrl.php index 002d46a90..c66c9ea3d 100644 --- a/tests/TestClassAddressRedirectIfOldUrl.php +++ b/tests/TestClassAddressRedirectIfOldUrl.php @@ -24,7 +24,7 @@ use DI\Container; use DI\ContainerBuilder; -use onOffice\WPlugin\Utility\AddressRedirector; +use onOffice\WPlugin\Controller\Redirector\AddressRedirector; class TestClassAddressRedirectIfOldUrl extends \WP_UnitTestCase @@ -59,22 +59,6 @@ public function stestInstance() $this->assertInstanceOf( AddressRedirector::class, $this->_pRedirectIfOldUrl ); } - /** - * @covers \onOffice\WPlugin\Utility\AddressRedirector::getCurrentLink - */ - public function testGetCurrentLink() - { - $this->assertEquals( 'http://example.org/address-detail-view/123', $this->_pRedirectIfOldUrl->getCurrentLink() ); - } - - /** - * @covers \onOffice\WPlugin\Utility\AddressRedirector::getUri - */ - public function testGetUri() - { - $this->assertEquals( 'address-detail-view/123', $this->_pRedirectIfOldUrl->getUri() ); - } - /** * @covers \onOffice\WPlugin\Utility\AddressRedirector::redirectDetailView */ diff --git a/tests/TestClassRedirectIfOldUrl.php b/tests/TestClassEstateRedirectIfOldUrl.php similarity index 79% rename from tests/TestClassRedirectIfOldUrl.php rename to tests/TestClassEstateRedirectIfOldUrl.php index c49d90108..364611218 100644 --- a/tests/TestClassRedirectIfOldUrl.php +++ b/tests/TestClassEstateRedirectIfOldUrl.php @@ -24,16 +24,13 @@ use DI\Container; use DI\ContainerBuilder; -use onOffice\WPlugin\Controller\EstateDetailUrl; -use onOffice\WPlugin\Utility\Redirector; -use onOffice\WPlugin\WP\WPPageWrapper; -use onOffice\tests\RedirectWrapperMocker; +use onOffice\WPlugin\Controller\Redirector\EstateRedirector; -class TestClassRedirectIfOldUrl +class TestClassEstateRedirectIfOldUrl extends \WP_UnitTestCase { /** - * @var Redirector + * @var EstateRedirector */ private $_pRedirectIfOldUrl; @@ -50,7 +47,7 @@ public function prepare() $pContainerBuilder = new ContainerBuilder; $pContainerBuilder->addDefinitions( ONOFFICE_DI_CONFIG_PATH ); $this->_pContainer = $pContainerBuilder->build(); - $this->_pRedirectIfOldUrl = $this->_pContainer->get( Redirector::class ); + $this->_pRedirectIfOldUrl = $this->_pContainer->get( EstateRedirector::class ); $_SERVER['REQUEST_URI'] = '/detail-view/123'; } @@ -59,27 +56,11 @@ public function prepare() */ public function stestInstance() { - $this->assertInstanceOf( Redirector::class, $this->_pRedirectIfOldUrl ); + $this->assertInstanceOf( EstateRedirector::class, $this->_pRedirectIfOldUrl ); } /** - * @covers \onOffice\WPlugin\Utility\Redirector::getCurrentLink - */ - public function testGetCurrentLink() - { - $this->assertEquals( 'http://example.org/detail-view/123', $this->_pRedirectIfOldUrl->getCurrentLink() ); - } - - /** - * @covers \onOffice\WPlugin\Utility\Redirector::getUri - */ - public function testGetUri() - { - $this->assertEquals( 'detail-view/123', $this->_pRedirectIfOldUrl->getUri() ); - } - - /** - * @covers \onOffice\WPlugin\Utility\Redirector::redirectDetailView + * @covers \onOffice\WPlugin\Controller\Redirector\EstateRedirector::redirectDetailView */ public function testRedirectDetailViewSameUrl() { @@ -102,7 +83,7 @@ public function testRedirectDetailViewSameUrl() } /** - * @covers \onOffice\WPlugin\Utility\Redirector::redirectDetailView + * @covers \onOffice\WPlugin\Controller\Redirector\EstateRedirector::redirectDetailView */ public function testRedirectDetailViewNotMatchRule() { diff --git a/tests/TestClassRedirector.php b/tests/TestClassRedirector.php new file mode 100644 index 000000000..f78ff3758 --- /dev/null +++ b/tests/TestClassRedirector.php @@ -0,0 +1,77 @@ +. + * + */ + +declare (strict_types=1); + +namespace onOffice\tests; + +use DI\Container; +use DI\ContainerBuilder; +use onOffice\WPlugin\Utility\Redirector; + +class TestClassRedirector + extends \WP_UnitTestCase +{ + /** + * @var Redirector + */ + private $_pRedirector; + + /** @var Container */ + private $_pContainer; + + /** + * @before + */ + public function prepare() + { + global $wp; + $wp->request = 'address-detail-view/123'; + $pContainerBuilder = new ContainerBuilder; + $pContainerBuilder->addDefinitions( ONOFFICE_DI_CONFIG_PATH ); + $this->_pContainer = $pContainerBuilder->build(); + $this->_pRedirector = $this->_pContainer->get( Redirector::class ); + $_SERVER['REQUEST_URI'] = '/address-detail-view/123'; + } + + /** + * + */ + public function stestInstance() + { + $this->assertInstanceOf( Redirector::class, $this->_pRedirector ); + } + + /** + * @covers \onOffice\WPlugin\Utility\Redirector::getCurrentLink + */ + public function testGetCurrentLink() + { + $this->assertEquals( 'http://example.org/address-detail-view/123', $this->_pRedirector->getCurrentLink() ); + } + + /** + * @covers \onOffice\WPlugin\Utility\Redirector::getUri + */ + public function testGetUri() + { + $this->assertEquals( 'address-detail-view/123', $this->_pRedirector->getUri() ); + } +} From f551f8c12bdfd328486a991a3d9f82a269542a86 Mon Sep 17 00:00:00 2001 From: andernath Date: Tue, 22 Oct 2024 10:58:00 +0200 Subject: [PATCH 8/8] Fix unit tests p#119769 --- tests/TestClassAddressRedirectIfOldUrl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestClassAddressRedirectIfOldUrl.php b/tests/TestClassAddressRedirectIfOldUrl.php index c66c9ea3d..44bd0f12f 100644 --- a/tests/TestClassAddressRedirectIfOldUrl.php +++ b/tests/TestClassAddressRedirectIfOldUrl.php @@ -60,7 +60,7 @@ public function stestInstance() } /** - * @covers \onOffice\WPlugin\Utility\AddressRedirector::redirectDetailView + * @covers \onOffice\WPlugin\Controller\Redirector\AddressRedirector::redirectDetailView */ public function testRedirectDetailViewSameUrl() { @@ -73,7 +73,7 @@ public function testRedirectDetailViewSameUrl() } /** - * @covers \onOffice\WPlugin\Utility\AddressRedirector::redirectDetailView + * @covers \onOffice\WPlugin\Controller\Redirector\AddressRedirector::redirectDetailView */ public function testRedirectDetailViewNotMatchRule() {