markhuot / craft-pest

https://craft-pest.com
Other
38 stars 11 forks source link

Craft Commerce: 404 on form request without action #31

Closed ostark closed 1 year ago

ostark commented 1 year ago

This test does not pass

<?php

test('checkout as a guest', function() {

    $this->get('/product/san-quentin-24')
        ->assertOk()
        ->form('#buy form')
        ->submit()
        ->assertOk();

});

Since there is no action attribute on the form element, I tried to tweak it the submit() method

        $uri = $this->form->getUri();

        if ("" == $this->form->getFormNode()->getAttribute('action')) {
              // use previous uri  
              $uri = \Craft::$app->getRequest()->getUrl();
        }

        $request = new RequestBuilder(
            $this->form->getMethod(),
            $uri
        );

... but without success.

the ->getFields() output looks okay

 Array &0 (
      'CRAFT_CSRF_TOKEN' => 'G6qIJCT8krJi_fc...='
      'action' => 'commerce/cart/update-cart'
      'successMessage' => 'ffe3ef7acc2f7371...4eb8e23c0df3Added San Quentin 24 to the cart.'
      'purchasableId' => '325'
  )

Any idea?

ostark commented 1 year ago

The full demo to understand the issue better https://github.com/fortrabbit/spoke-and-chain-pestphp/tree/pest/tests/Feature

markhuot commented 1 year ago

I'll clone it down and have a look. I have a // @todo to better handle URL/URI parsing in the request builder so I think I know where Yii is getting tripped up.

ostark commented 1 year ago

So far submit() creates a new POST request, follows a redirect, and returns a 200 OK, which is a bit unexpected to me.

I'd prefer something like this

# no redirect, but assertions allowed
->submit()
->assertRedirectTo('/checkout/next-step')
# to support further chaining
->submit()
->assertRedirectTo('/checkout/next-step')
->follow()
markhuot commented 1 year ago

Well that is surprising. I'm not sure why it's following that redirect. I'll take a look. Agreed that being explicit about following redirects is the right choice.

markhuot commented 1 year ago

Yuck, I believe the issue is that Craft's {% redirect %} tag (and other redirect methods) throw a ->end() in there which is short circuiting the RequestHandler's logic. Will look for an override.

image

It's that ->end() on line 39 of this compiled Twig template that causes issues.

ostark commented 1 year ago

OverwriteEverything ™

markhuot commented 1 year ago

Yup, I have a fix. There's a YII_ENV_TEST var that skips the exit() I just need to catch the exception that is thrown instead. Working on the ->follow() side of things next.

markhuot commented 1 year ago

Should be fixed with https://github.com/markhuot/craft-pest/pull/33. Waiting on the test suite to run.

markhuot commented 1 year ago

This is merged. I'm going to close this issue. Feel free to reopen if you're still having problems.