/**
  * @dataProvider serializeToJsonProvider
  */
 public function testSerializeToJson($sourceNodeType, $expectedFile)
 {
     $serializer = new NodeTypeJsonSerializer();
     $json = $serializer->serialize($sourceNodeType);
     // Assert
     $this->assertJsonStringEqualsJsonFile($expectedFile, $json);
 }
 /**
  * Import a Json file (.rzt) containing setting and setting group.
  *
  * @param string $serializedData
  * @param EntityManager $em
  *
  * @return bool
  */
 public static function importJsonFile($serializedData, EntityManager $em)
 {
     $return = false;
     $serializer = new NodeTypeJsonSerializer();
     $nodeType = $serializer->deserialize($serializedData);
     $existingNodeType = $em->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findOneByName($nodeType->getName());
     if ($existingNodeType === null) {
         $em->persist($nodeType);
         $existingNodeType = $nodeType;
         foreach ($nodeType->getFields() as $field) {
             $em->persist($field);
             $field->setNodeType($nodeType);
         }
     } else {
         $existingNodeType->getHandler()->diff($nodeType);
     }
     $em->flush();
     $existingNodeType->getHandler()->regenerateEntityClass();
     return $return;
 }
 /**
  * Import a Json file (.rzt) containing NodeType datas and fields.
  *
  * @param Symfony\Component\HttpFoundation\Request $request
  *
  * @return Symfony\Component\HttpFoundation\Response
  */
 public function importJsonFileAction(Request $request)
 {
     $this->validateAccessForRole('ROLE_ACCESS_NODETYPES');
     $form = $this->buildImportJsonFileForm();
     $form->handleRequest($request);
     if ($form->isValid() && !empty($form['node_type_file'])) {
         $file = $form['node_type_file']->getData();
         if ($file->isValid()) {
             $serializedData = file_get_contents($file->getPathname());
             if (null !== json_decode($serializedData)) {
                 $serializer = new NodeTypeJsonSerializer();
                 $nodeType = $serializer->deserialize($serializedData);
                 $existingNT = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findOneBy(['name' => $nodeType->getName()]);
                 if (null === $existingNT) {
                     /*
                      * New node-type…
                      *
                      * First persist node-type
                      */
                     $this->getService('em')->persist($nodeType);
                     // Flush before creating node-type fields.
                     $this->getService('em')->flush();
                     foreach ($nodeType->getFields() as $field) {
                         /*
                          * then persist each field
                          */
                         $field->setNodeType($nodeType);
                         $this->getService('em')->persist($field);
                     }
                     $msg = $this->getTranslator()->trans('nodeType.imported.created');
                     $this->publishConfirmMessage($request, $msg);
                 } else {
                     /*
                      * Node-type already exists.
                      * Must update fields.
                      */
                     $existingNT->getHandler()->diff($nodeType);
                     $msg = $this->getTranslator()->trans('nodeType.imported.updated');
                     $this->publishConfirmMessage($request, $msg);
                 }
                 $this->getService('em')->flush();
                 $nodeType->getHandler()->updateSchema();
                 /*
                  * Redirect to update schema page
                  */
                 return $this->redirect($this->generateUrl('nodeTypesSchemaUpdate', ['_token' => $this->getService('csrfTokenManager')->getToken(static::SCHEMA_TOKEN_INTENTION)]));
             } else {
                 $msg = $this->getTranslator()->trans('file.format.not_valid');
                 $request->getSession()->getFlashBag()->add('error', $msg);
                 $this->getService('logger')->error($msg);
                 // redirect even if its null
                 return $this->redirect($this->generateUrl('nodeTypesImportPage'));
             }
         } else {
             $msg = $this->getTranslator()->trans('file.not_uploaded');
             $request->getSession()->getFlashBag()->add('error', $msg);
             $this->getService('logger')->error($msg);
             // redirect even if its null
             return $this->redirect($this->generateUrl('nodeTypesImportPage'));
         }
     }
     $this->assignation['form'] = $form->createView();
     return $this->render('node-types/import.html.twig', $this->assignation);
 }