Sign in to follow this  
---

Page rootParent override

Recommended Posts

---    2

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
kixe    809

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 benbyf
      PW Admin doesn't really function without javascript, and my question would be, should we care?
      I think, however, either way it might be nice to add a "<span>Javascript doesn't seem to be available, therefore you may not be able to access the full functionality of the admin</span>" somewhere maybe?
    • By Martin Muzatko
      See gif:

      When I use $sanitizer->pageName()  to sanitize a title as name, I get a different result from what processwire automatically replaces, when editing the name in the CMS.
      I have to manually replace the dashes and even dots!
      I would expect the name to be equal to the last segment of the pagepath.
        
    • By Davidov
      Good Day!
      I am a beginer of processwire project. Thanks developers for your great work. this CMS is very Good!
      I have a question: how get Pageimage object of PageArray from "Page" (PageArray) field type?
      I have too tamplates: affiliates and their teachers. The template of affiliates have a field "teachers_list" wich is "Page" (PageArray) type.
      When I vaffiliate teachers in a cycle, I can not access the teacher photo object and change its size.   I get an error returning: " Fatal error: Call to a member function maxWidth() on null ". When i trying get URL attribute : $teacherItem->teacher_photo->url, i receive message: " Trying to get property of non-object ".   But this code works: $teacherItem->teacher_photo["url"].   The print_r function outputs the following information: "ProcessWire \ Pageimage Object ([changes] => Array ([0] => formatted) [hooks] => Array ([Pageimage :: pim2Load] => PageImageManipulator02-> getPageImageManipulator02 () in PageImageManipulator02.module ) [Data] => Array ([basename] => p16.jpg [description] => .... - ..... [tags] => [formatted] => 1 [modified] => 1487079115 [created ] => 1487079115))".   my PHP code: foreach ($page->teacher_list as $teacherItem): $out .= " <tr> <td class=\"alignTop\"> /* !!! my truble */ <img src=\"{$teacherItem->teacher_photo->maxWidth(250)->url}\" /> /* !!! my truble */ </td> <td class=\"alignTop\"> <div class=\"teach_style12\"> <strong>$teacherItem->fio</strong><br/> $teacherItem->degree<br/> $teacherItem->headline_detail </div> <div class=\"teach_style9\"> $teacherItem->body </div> </td> </tr>"; endforeach; Please, help me. Where is my error?
       
    • By louisstephens
      I guess this is the right place to post this. Just this morning I went to like an answer someone had given, and was met with a popup stating: "Unable to give reputation to this user". Has anyone encountered this before, or is there some "cap" on users with a certain ratio (reputation to posts) that is keeping me from providing reputation. It was not a big deal, but I wanted to give credit to where credit was due.
    • 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.