diff --git a/plugin.php b/plugin.php
index c0b68e206..2a7da53d4 100644
--- a/plugin.php
+++ b/plugin.php
@@ -66,6 +66,10 @@
use onOffice\WPlugin\Utility\Redirector;
use onOffice\WPlugin\WP\WPQueryWrapper;
use onOffice\WPlugin\ScriptLoader\IncludeFileModel;
+use onOffice\WPlugin\Record\AddressIdRequestGuard;
+use onOffice\WPlugin\Controller\Redirector\AddressRedirector;
+use onOffice\WPlugin\Controller\Redirector\EstateRedirector;
+use onOffice\WPlugin\Controller\AddressDetailUrl;
const DEFAULT_LIMIT_CHARACTER_TITLE = 60;
@@ -282,14 +286,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']);
@@ -393,7 +408,26 @@ 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);
+ }
+});
+
+$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 $pAddressIdGuard */
+ $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);
}
});
diff --git a/plugin/AddressList.php b/plugin/AddressList.php
index 846dae914..ae480b84a 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();
}
/**
@@ -568,17 +573,40 @@ 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();
+ $pageId = $this->_pEnvironment->getDataAddressDetailViewHandler()
+ ->getAddressDetailView()->getPageId();
+
+ $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 );
- if ( empty( $fullLinkElements['query'] ) ) {
- $url .= '/';
- }
- return $url;
+ $url = get_page_link( $pageId );
+ $fullLink = $this->_pLanguageSwitcher->createAddressDetailLink( $url, $addressId, $addressTitle );
+
+ $fullLinkElements = parse_url( $fullLink );
+ if ( empty( $fullLinkElements['query'] ) ) {
+ $fullLink .= '/';
+ }
+
+ return $fullLink;
}
/**
diff --git a/plugin/Controller/AddressDetailUrl.php b/plugin/Controller/AddressDetailUrl.php
new file mode 100644
index 000000000..a5ce7c1c0
--- /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/Controller/Redirector/AddressRedirector.php b/plugin/Controller/Redirector/AddressRedirector.php
new file mode 100644
index 000000000..ecddaf676
--- /dev/null
+++ b/plugin/Controller/Redirector/AddressRedirector.php
@@ -0,0 +1,63 @@
+_wpAddressDetailUrl = $addressDetailUrl;
+ $this->_wpRedirectWrapper = $redirectWrapper;
+ $this->_redirector = new Redirector();
+ }
+
+
+ /**
+ * @param int $addressId
+ * @param string $addressTitle
+ * @param bool $pAddressRedirection
+ * @return bool|void
+ */
+
+ public function redirectDetailView(int $addressId, string $addressTitle, bool $pAddressRedirection)
+ {
+ $matches = $this->_redirector->checkUrlIsMatchRule();
+ if (empty($matches[2])) {
+ return true;
+ }
+
+ $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->_redirector->checkNewUrlIsValid(
+ array_filter(explode('/', $newUrl)),
+ array_filter(explode('/', $oldUrl))
+ );
+
+ if ($isNewUrlValid) {
+ $this->_wpRedirectWrapper->redirect($newUrl);
+ }
+ }
+ }
+}
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 e1ab2cffa..96bb3606f 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 = __('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);
+ $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/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
new file mode 100644
index 000000000..00c7eace7
--- /dev/null
+++ b/plugin/Record/AddressIdRequestGuard.php
@@ -0,0 +1,245 @@
+.
+ *
+ */
+
+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\Controller\Redirector\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()[ $addressId ];
+
+ 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->getValue('Vorname');
+ $lastName = $this->_addressData->getValue('Name');
+ $company = $this->_addressData->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->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);
+ $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'],
+ '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/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/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,
];
diff --git a/tests/TestClassAddressDetailUrl.php b/tests/TestClassAddressDetailUrl.php
new file mode 100644
index 000000000..28d788962
--- /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 testAddressDetailUrl()
+ {
+ 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 testAddressDetailUrlNotSetOptionShowUrlAndTitle()
+ {
+ 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 testAddressDetailUrlAndParameter()
+ {
+ 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 1684cf1ae..fc99386de 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
@@ -217,7 +223,7 @@ public function prepare()
->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));
@@ -242,8 +248,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);
}
@@ -355,6 +362,39 @@ public function testGetFieldType()
$this->_pAddressList->getFieldType('HerkunftKontakt'));
}
+ /**
+ *
+ */
+
+ public function testGetAddressLink()
+ {
+ add_option('onoffice-address-detail-view-showInfoUserUrl', true);
+ 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/13-fred-firestone/', $this->_pAddressList->getAddressLink("13"));
+ }
/**
*
@@ -514,6 +554,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..44bd0f12f
--- /dev/null
+++ b/tests/TestClassAddressRedirectIfOldUrl.php
@@ -0,0 +1,100 @@
+.
+ *
+ */
+
+declare (strict_types=1);
+
+namespace onOffice\tests;
+
+use DI\Container;
+use DI\ContainerBuilder;
+use onOffice\WPlugin\Controller\Redirector\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\Controller\Redirector\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\Controller\Redirector\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);
+ }
+}
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() );
+ }
+}