Jump to content

Page rootParent override


---
 Share

Recommended Posts

I'm trying to create tests using PHPUnit, and I have the following method:

protected function _getRootItems(Page $currentPage)
    {
        /** @var Page $page */
        foreach (wire('pages')->find("has_parent!=2,id!=2|7,status<" . Page::statusTrash . ",include=all") as $page) {
            if ($this->_isActivePage($page) && $page->parent_id == 1) {
                $output[$page->sort] = [
                    'id' => $page->id,
                    'title' => $page->title,
                    'url' => $page->url,
                    'template' => $page->template->name,
                    'isActive' => $page->id == $currentPage->rootParent->id,
                    'children' => []
                ];
            }
        }

        ksort($output);

        return $output;
    }

I want to write a test for this method in PHPUnit. I end up with the error: Trying to get property of non-object

This is because $currentPage->rootParent is null.

My test looks like this:

public function testGetRootItems()
    {
        $mock = $this
            ->__getMock()
            ->disableOriginalConstructor()
            ->setMethods([
                '_isActive'
            ])
            ->getMock();

        $mock
            ->expects($this->any())
            ->method('_isActive')
            ->willReturn(false);

        $page = $this
            ->getMockBuilder(\ProcessWire\Page::class)
            ->disableOriginalConstructor()
            ->setMethods([
                '__get'
            ])
            ->getMock();

        $rootParent = $this
            ->getMockBuilder(\ProcessWire\Page::class)
            ->disableOriginalConstructor()
            ->setMethods([
                '__get'
            ])
            ->getMock();
        $template = $this
            ->getMockBuilder(\ProcessWire\Template::class)
            ->disableOriginalConstructor()
            ->setMethods([
                '__get'
            ])
            ->getMock();

        $template->name = 'name of the template';

        $rootParent->id = 1;
        $page->template = $template;
        $page->id = 2;
        $page->rootParent = 'hierzo!';

        var_dump(
            $page->rootParent
        );


        $method = $this->__getReflectionMethod('_getRootItems', $mock);

        $method->invoke(
            $mock,
            $page
        );
    }

I've already tried to override the rootParent with it's method (___rootParent), tried setting it directly ($page->rootParent) but so far nothing worked. I probably miss something really simple here.

I know that in my example, I set the value to a string. But the result of the var_dump below it is still NULL. I also tried setting it using $page->rootParent = $rootParent, this had no effect.

 

So my question: How can I possible override this rootParent variable?

Link to comment
Share on other sites

You can set the parent, but not the rootParent.  RootParent is determined on the base of the pages parent. Method rootParent() is a getter and not setter.
Yes, you are able to modify the rootParent with a hook. But the hook option is not meant to change the rootParent and NOT the parent. (which would result in unexpected conflicts).

Solution: change the parent.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...