Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-4287 - Add validation for some pages #17

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Validator/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Wishlist implements ValidatorInterface
use PathTrait;

const SHARED_URL_KEY = 'shared';
const WISLIST_URL_KEY = 'wishlist';

/**
* @var WishlistResourceModel
Expand All @@ -42,8 +43,16 @@ public function __construct(WishlistResourceModel $wishlistResource, WishlistFac
$this->wishlistFactory = $wishlistFactory;
}

/**
* @param RequestInterface $request
* @return bool
*/
public function validateRequest(RequestInterface $request): bool
{
if ($this->checkIfAccountWishlistTab($request)) {
return true;
}

$urlKey = $this->getPathFrontName($request);

if ($urlKey !== self::SHARED_URL_KEY) {
Expand All @@ -55,6 +64,10 @@ public function validateRequest(RequestInterface $request): bool
return $this->checkIsShared($sharingKey);
}

/**
* @param string $sharingKey
* @return bool
*/
protected function checkIsShared(string $sharingKey): bool
{
/** @var WishlistModel $wishlist */
Expand All @@ -68,11 +81,31 @@ protected function checkIsShared(string $sharingKey): bool
return true;
}

/**
* @param RequestInterface $request
* @return string
*/
protected function getSharingKey(RequestInterface $request): string
{
$path = trim($request->getPathInfo(), '/');
$params = explode('/', $path);

return end($params);
}

/**
* @param RequestInterface $request
* @return bool
*/
public function checkIfAccountWishlistTab(RequestInterface $request): bool
{
$path = trim($request->getPathInfo(), '/');
$params = explode('/', $path);

if (count($params) === 1 && $params[0] === self::WISLIST_URL_KEY) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not recommending to fix the param count to 1
some 3rd party extension or project customization could easily transform it to /wishlist/...whatever.
it may be safer to only check if the 0th element is WISLIST_URL_KEY

return true;
}

return false;
}
}
4 changes: 4 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<item name="wishlist" xsi:type="string">ScandiPWA\Router\Validator\Wishlist</item>
<item name="checkout" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>
<item name="compare" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>
<item name="customer" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>
<item name="sales" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>
<item name="search" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to always pass search? what if there's no results?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, also though about it. But since when no items found in search it actually shows not 404 I decided to stay as 200. It just says no products found.
Also just checked on magento demo and seems same results on how magento acts.
https://magento2-demo.magebit.com/catalogsearch/result/?q=testest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it will be better to add this change, for consistency.
I'm not sure what is the desired behavior, though.

Earlier PWA would return 404 on PLP's on no products, and 200 if there were products.
Search behaves the same way now:
https://luma-demo.scandipwa.com/search/yoloqwe <--- 404
https://luma-demo.scandipwa.com/search/yoloqwe <-- 200

However, it looks like at some point it was changed, and now PLP's always return 200.
Then we may as well do the same for search.
However, if we do that (and it is a desired result), IMO would need to remove the 200/404 status check logic that relates to product count, as that's extra logic that wouldn't be used.

Overall -> I'd consult SEO team, what they want there, 200 or 404, and go with that on both PLP and search.

<item name="newsletter" xsi:type="string">ScandiPWA\Router\Validator\AlwaysPass</item>
</argument>
</arguments>
</type>
Expand Down