Sign in to follow this  
Followers 0
---

Page rootParent override

2 posts in this topic

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?

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mirza
      Hi All,
      I have created a script which will create the pages and it should be unpublished.
      I have added the status as unpublished by default while first time saving the page as below.
      $hotelPage->hotel_website = $hotel['hotelWebsite']; // By default page created will be unpublished state $hotelPage->addStatus(Page::statusUnpublished); $hotelPage->save(); $hotelPage->setOutputFormatting(false); // Add hotel images only after setting output formatting to false $hotelPage->images->add($hotel['hotelImage']); $hotelPage->save(); On the same time, saving the hotel image as well and triggering the save method.
      But when I see the created page in the tree, Its shows unpublished but publish_date is not empty(date will be present as created, modified and published are all same)
      Now I don't understand why the date is getting filled into the publish field for a page?
      Please let me know if am doing something wrong.
    • By Herrmann22
      Hi,
      i build at the moment my first website with ProcessWire. Now I want integrate a 3D configurator like on the following site.
      Know anywhere a PlugIn or another module with them I can realize my project? Thanks for help.
    • By Michael Steinmann
      Hello everyone,
      i guess my php knowledge is not good enough to find a solution for the following problem:
      I'm building a website where i have projects. Each project has tags like "webdesign, responsive, cms, print, flyer" etc. At the end of a project i want to recommend other projects, which have similar tags. I want to limit the recommendations to three other projects sorted by the hightest amount of same tags.
      This is how far i got:
      I'm using
      <?php $interessantes = $pages->find("tags=$page->tags, id!=$page->id"); ?> to find other projects with the same tags, excluding the page i'm already on.
      Then i'm using three foreach-functions to go through the projects i found, the tags they have and compare them with the tags of the project i'm looking at. If i have two identical tags, i count up a variable.
      <?php // Projects i have found foreach($interessantes as $interessant): $i = 0; // Tags of the projects i have found foreach($interessant->tags as $tag): // Tags of the project i'm looking at foreach($page->tags as $tagreferenz): if($tag == $tagreferenz): $i++; endif; endforeach; endforeach; endforeach; ?> You can see what it looks like in the screenshot with a bit of HTML. I marked the tags of the current project green and the identical tags of other projects red with the amount of hits below. So in this example i would want to have three projects with three similar tags and get rid of those with only two and one.

      I guess i have to put my pages into an array (with the number of hits?), sort them and echo them with "limit=3"? Unfortunately i have no idea how to do this. You probably have an even better/shorter solution. Links to other topics are welcome to and i'll try to get my head around it.
      If you need further explanation, i'll try my best.
      Best regards
    • By didhavn
      Hey all. 
      This might sound a bit strange but I am looking for a solution to hook into page edit and remove or disable the two save buttons including their dropdown options. 
      I managed to partly remove the buttons, but the dropdowns remained. However, I would prefer a solution with disabled buttons but have no idea how to achieve this. 
      I hope somebody of you can tell me how to remove or disable these buttons. 
      Thanks a lot in advance 
    • By Cody Connor
      I installed the InputfieldCKEditor module and am using the ck editor in all of the fields on my processwire website.  I need to be able to drag images into the CKEditor field so I downloaded a CKEditor plugin called simpleuploads put it into /site/modules/InputfieldCKEditor/plugins/ and added the plugin to the field I am working on.  I have tried doing it many different ways and the plugins still are not working and I have no idea where to go from here.
      I am working with processwire version 3.0.61.
      I am using CKEditor version 4.4.2.