public void GetItem_PhysicalPathDoesNotStartWithContentRoot_ReturnsNull()
        {
            var fileProvider = new TestFileProvider("BasePath2");
            var file1        = fileProvider.AddFile("/File1.cshtml", "content");
            var file2        = fileProvider.AddFile("/File2.js", "content");
            var file3        = fileProvider.AddFile("/File3.cshtml", "content");

            fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2, file3 });

            var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);

            var razorProject = new FileProviderRazorProject(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));

            // Act
            var item = razorProject.GetItem("/File3.cshtml");

            // Assert
            Assert.True(item.Exists);
            Assert.Equal("/File3.cshtml", item.FilePath);
            Assert.Equal(string.Empty, item.BasePath);
            Assert.Equal(Path.Combine("BasePath2", "File3.cshtml"), item.PhysicalPath);
            Assert.Null(item.RelativePhysicalPath);
        }
        public void OnProvidersExecuting_DoesNotAddPagesUnderAreas_WhenFeatureIsDisabled()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("Categories.cshtml", "@page");
            var file2        = fileProvider.AddFile("Index.cshtml", "@page");
            var file3        = fileProvider.AddFile("List.cshtml", "@page \"{sortOrder?}\"");
            var file4        = fileProvider.AddFile("About.cshtml", "@page");
            var manageDir    = fileProvider.AddDirectoryContent("/Areas/Products/Pages/Manage", new[] { file1 });
            var areaPagesDir = fileProvider.AddDirectoryContent("/Areas/Products/Pages", new IFileInfo[] { manageDir, file2, file3, });
            var productsDir  = fileProvider.AddDirectoryContent("/Areas/Products", new[] { areaPagesDir });
            var areasDir     = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir });
            var pagesDir     = fileProvider.AddDirectoryContent("/Pages", new[] { file4 });
            var rootDir      = fileProvider.AddDirectoryContent("/", new[] { areasDir, pagesDir });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = false
            });
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
            });
        }
Exemple #3
0
    public void EnumerateFiles_IteratesOverAllCshtmlUnderRoot()
    {
        // Arrange
        var fileProvider = new TestFileProvider("BasePath");
        var directory1   = new TestDirectoryFileInfo
        {
            Name = "Level1-Dir1",
        };
        var file1      = fileProvider.AddFile("File1.cshtml", "content");
        var directory2 = new TestDirectoryFileInfo
        {
            Name = "Level1-Dir2",
        };

        fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 });

        var file2      = fileProvider.AddFile("/Level1-Dir1/File2.cshtml", "content");
        var file3      = fileProvider.AddFile("/Level1-Dir1/File3.cshtml", "content");
        var file4      = fileProvider.AddFile("/Level1-Dir1/File4.txt", "content");
        var directory3 = new TestDirectoryFileInfo
        {
            Name = "Level2-Dir1"
        };

        fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 });
        var file5 = fileProvider.AddFile(Path.Combine("Level1-Dir2", "File5.cshtml"), "content");

        fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 });
        fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]);

        var fileSystem = GetRazorProjectFileSystem(fileProvider);

        // Act
        var razorFiles = fileSystem.EnumerateItems("/");

        // Assert
        Assert.Collection(razorFiles.OrderBy(f => f.FilePath),
                          file =>
        {
            Assert.Equal("/File1.cshtml", file.FilePath);
            Assert.Equal("/", file.BasePath);
            Assert.Equal(Path.Combine("BasePath", "File1.cshtml"), file.PhysicalPath);
            Assert.Equal("File1.cshtml", file.RelativePhysicalPath);
        },
                          file =>
        {
            Assert.Equal("/Level1-Dir1/File2.cshtml", file.FilePath);
            Assert.Equal("/", file.BasePath);
            Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File2.cshtml"), file.PhysicalPath);
            Assert.Equal(Path.Combine("Level1-Dir1", "File2.cshtml"), file.RelativePhysicalPath);
        },
                          file =>
        {
            Assert.Equal("/Level1-Dir1/File3.cshtml", file.FilePath);
            Assert.Equal("/", file.BasePath);
            Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath);
            Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath);
        },
                          file =>
        {
            Assert.Equal("/Level1-Dir2/File5.cshtml", file.FilePath);
            Assert.Equal("/", file.BasePath);
            Assert.Equal(Path.Combine("BasePath", "Level1-Dir2", "File5.cshtml"), file.PhysicalPath);
            Assert.Equal(Path.Combine("Level1-Dir2", "File5.cshtml"), file.RelativePhysicalPath);
        });
    }
        public void OnProvidersExecuting_AddsPagesUnderAreas()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("Categories.cshtml", "@page");
            var file2        = fileProvider.AddFile("Index.cshtml", "@page");
            var file3        = fileProvider.AddFile("List.cshtml", "@page \"{sortOrder?}\"");
            var file4        = fileProvider.AddFile("_ViewStart.cshtml", "@page");
            var manageDir    = fileProvider.AddDirectoryContent("/Areas/Products/Pages/Manage", new[] { file1 });
            var pagesDir     = fileProvider.AddDirectoryContent("/Areas/Products/Pages", new IFileInfo[] { manageDir, file2, file3, file4 });
            var productsDir  = fileProvider.AddDirectoryContent("/Areas/Products", new[] { pagesDir });
            var areasDir     = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir });
            var rootDir      = fileProvider.AddDirectoryContent("/", new[] { areasDir });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = true
            });
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Manage/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Manage/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Manage/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Manage/Categories", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Index.cshtml", model.RelativePath);
                Assert.Equal("/Index", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Index", selector.AttributeRouteModel.Template),
                                  selector => Assert.Equal("Products", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Index", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/List.cshtml", model.RelativePath);
                Assert.Equal("/List", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/List/{sortOrder?}", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/List", kvp.Value);
                });
            });
        }
Exemple #5
0
        public void EnumerateFiles_IteratesOverAllCshtmlUnderRoot()
        {
            // Arrange
            var fileProvider = new TestFileProvider("BasePath");
            var directory1   = new TestDirectoryFileInfo
            {
                Name = "Level1-Dir1",
            };
            var file1      = fileProvider.AddFile("File1.cshtml", "content");
            var directory2 = new TestDirectoryFileInfo
            {
                Name = "Level1-Dir2",
            };

            fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 });

            var file2      = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File2.cshtml"), "content");
            var file3      = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File3.cshtml"), "content");
            var file4      = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File4.txt"), "content");
            var directory3 = new TestDirectoryFileInfo
            {
                Name = "Level2-Dir1"
            };

            fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 });
            var file5 = fileProvider.AddFile(Path.Combine("Level1-Dir2", "File5.cshtml"), "content");

            fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 });
            fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]);

            var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);

            var razorProject = new FileProviderRazorProject(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));

            // Act
            var razorFiles = razorProject.EnumerateItems("/");

            // Assert
            Assert.Collection(razorFiles.OrderBy(f => f.FilePath),
                              file =>
            {
                Assert.Equal("/File1.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "File1.cshtml"), file.PhysicalPath);
                Assert.Equal("File1.cshtml", file.RelativePhysicalPath);
            },
                              file =>
            {
                Assert.Equal("/Level1-Dir1/File2.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File2.cshtml"), file.PhysicalPath);
                Assert.Equal(Path.Combine("Level1-Dir1", "File2.cshtml"), file.RelativePhysicalPath);
            },
                              file =>
            {
                Assert.Equal("/Level1-Dir1/File3.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath);
                Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath);
            },
                              file =>
            {
                Assert.Equal("/Level1-Dir2/File5.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "Level1-Dir2", "File5.cshtml"), file.PhysicalPath);
                Assert.Equal(Path.Combine("Level1-Dir2", "File5.cshtml"), file.RelativePhysicalPath);
            });
        }
Exemple #6
0
        public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var conformingFileUnderAreasDirectory = fileProvider.AddFile("Categories.cshtml", "@page");
            // We shouldn't add a route for this.
            var nonConformingFileUnderAreasDirectory = fileProvider.AddFile("Home.cshtml", "@page");
            var rootFile = fileProvider.AddFile("About.cshtml", "@page");

            var productsDir = fileProvider.AddDirectoryContent("/Areas/Products", new[] { conformingFileUnderAreasDirectory });
            var areasDir    = fileProvider.AddDirectoryContent("/Areas", new IFileInfo[] { productsDir, nonConformingFileUnderAreasDirectory });
            var rootDir     = fileProvider.AddDirectoryContent("/", new IFileInfo[] { areasDir, rootFile });

            var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment);

            var optionsManager = Options.Create(new RazorPagesOptions
            {
                RootDirectory     = "/",
                AreaRootDirectory = "/Areas",
                AllowAreas        = true,
            });
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Categories", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/About", kvp.Value);
                });
            });
        }