Sign in to follow this  
---

Page rootParent override

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?

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mirza
      Any plans to support for processwire with php 7?
    • By Mirza
      Hi All,
      Arabic content changes not available in page getChanges method
      While editing the page from admin panel in the multi-language field, added a hook to save the changes made to the page.
       
      $this->pages->addHookAfter('saveReady', $this, 'hookUpdateLog');  
      public function hookUpdateLog(HookEvent $event) { $page = $event->arguments[0]; if(in_array($page->template->name, $this->templateList)) { $this->logFieldChanges($page); } } public function logFieldChanges($page) { $changes = $page->getChanges(); //This changes does not contain the field of arabic changes. }
      Please let me know, If I am approaching in a right way.

    • By flashmaster
      Hi im new to this forum and i need some help with a booking system that i have trying to implement under Processwire. I hope that i can explain as much as i can what my problem is.
      Here is the orginal script https://github.com/olejon/phpmyreservation
      I downloaded the script and got some help to fix it because it was all messed up. I have attached a file "ReservationSystem.rar" and it should work out of the box. Also attached the database file "Phpmyreservation.sql".
      The thing is the script works great as it is now but when i install a blank version of processwire and import the script to the template folder it wont work. I have tried many things but cant get it to work just error after error etc. Please ask me if you need some more information about this. I know you are the experts so thats why im asking you.
      ReservationSystem.rar
      phpmyreservation.sql
    • By zaib
      I've a simple scenario, i want to add two users "content_creator" & "content_publisher"
      content_creator can add new pages,edit them(only edit not publish) and view only where content_publisher can publish the unpublished pages after reviewing.
       
      Thank you.
    • By ov3rtak3r
      Hello everyone!
      I am new to Processwire and really like it, but now I have a problem that I can't solve by myself.
      I have created following templates:
      data_people (no template-file)
      person_listing (person_listing.php)
      person_detail (person_detail.php)

      I have added the following Pages:
      Actor, Director, Stuff using template person_listing.php [visible in frontend]
      "People" using template data_people [not visible, only for data]
      -> "Georg Klonee" using template person_detail [is visible, but should not be shown using path /en/person/georg-klonee]

      Example Person: Georg Klonee
      The Problem:
      He is a Actor and a Director, so I want him to be shown in the Listing of /en/actors and /en/directors. No problem until here - it works. But now, when I click him, the URL for the Detail-Page is unfortunately /en/person/georg-klonee. But I want it to be /en/actors/georg-klonee, if i open the details from Actors-Page (and of course /en/directors/georg-klonee when on Directors-Page).
      How can I get this to work? I am also using TemplateEngine-Module with TemplateTwigEngine-Module, hopefully this won't be a problem? I tried it with URL-Segments for person_listing.php. But how can I abort the rendering of people_listing and start rendering person_detail.php?
       
      Thanks for your help in advance.